When the user enters an input, it would show up on the console. How do I go around to making sure that it won't show up? I don't want to clear the entire screen as well.
1 2 3 4 5 6 7 8 9
if (_kbhit())
{
// I need something here to delete the line I would assume
cout << endl;
std::string inputstring;
getline(std::cin, inputstring);
sprintf_s(MessageBuffer, inputstring.c_str());
sendTalkPackets();
}
#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 (unsignedint 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;
}
It's not bomb-proof & it has some quirks that I don't understand, but it's a start. If you want the user to be able to see what they are typing, but delete it after they've finished, then the above will work. If you want to mask the user input (so that they do not see what they are typing), then i think this ( http://www.cplusplus.com/forum/general/3570/#msg15410 ) is what you need, though I've never tried it.