1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
#include <iostream>
#include <limits>
#include <windows.h>
#include <string>
void gotoxy( int column, int line )
{
COORD coord;
coord.X = column;
coord.Y = line;
SetConsoleCursorPosition(
GetStdHandle( STD_OUTPUT_HANDLE ),
coord
);
}
int wherex()
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
if (!GetConsoleScreenBufferInfo(
GetStdHandle( STD_OUTPUT_HANDLE ),
&csbi
))
return -1;
return csbi.dwCursorPosition.X;
}
int wherey()
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
if (!GetConsoleScreenBufferInfo(
GetStdHandle( STD_OUTPUT_HANDLE ),
&csbi
))
return -1;
return csbi.dwCursorPosition.Y;
}
int main ()
{
// First, lets get a feel for how the positions work & where we are
int posX = wherex();
int posY = wherey();
std::cout << "Starting position is " << posX << " , " << posY << std::endl;
std::cout << "Hello World (with newline)" << std::endl;
posX = wherex();
posY = wherey();
std::cout << "Position after the above is " << posX << " , " << posY << std::endl;
std::cout << "Hello World (without newline)";
posX = wherex();
posY = wherey();
std::cout << "\nPosition after the above is " << posX << " , " << posY << std::endl;
// Now lets try getting some user input, then deleting it!
std::cout << "Please enter something for me to destroy:" << std::endl;
// Get position of cursor where the user will start their input
int posXUserStart = wherex();
int posYUserStart = wherey();
// Get user input
std::string userString;
std::cin >> userString;
// Get position of cursor after user has finished
int posXUserEnd = wherex();
int posYUserEnd = wherey();
// Set Curser position to start of user input
gotoxy(posXUserStart , posYUserStart);
// Overwrite all of the user's string with spaces
for (unsigned int i = 0 ; i < userString.size() ; i++)
std::cout << ' ';
// Set Curser position to end of user input
gotoxy(posXUserEnd , posYUserEnd);
std::cout << "You entered \"" << userString
<< "\" but I destroyed it with spaces. Aren't I evil?" << std::endl;
// Wait for user prompt to exit the program
std::cout << "Press <return> to exit" << std::endl;
// Why does this need to be done twice?
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return 0;
}
| |