How to delete line after a cin?

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();
	}
Perhaps the backspace character '\b' will be your friend?

1
2
3
  std::cout << "bye!";
  
  std::cout << "\b\b\b\b" << "    " << "\b\b\b\b";


This thread may give you more answers: http://www.cplusplus.com/forum/general/3570/
It didn't work. I tried to backspace multiple times, but there was no change.

As for the thread, its too deep for me :l
Last edited on
Windows-specific, but borrowing from this thread ( http://www.cplusplus.com/forum/general/74380/ ) I have the following:

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;
}


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.
Topic archived. No new replies allowed.