Reputation: 377
After clearing invalid input with std::cin.clear() and std::cin.ignore(), the cursor moves to the beginning of the next line printed by std::cout. I experimented with including an extra std::endl in various positions, to no avail.
Is there any way to force the cursor to the end of the line?
Prompt
do {
std::cout << "Enter a number: ";
if(!(std::cin >> number)) {
std::cout << "Not a number. Try again..."<< std::endl;
std::cin.clear();
std::cin.ignore(10000, '\n');
}
} while (number != -1);
Output
Enter a number: |
Invalid input. Try again...
|Enter a number:
Upvotes: 1
Views: 850
Reputation: 6983
std::cout isn't always what you think it is. It's not "the output to the terminal" - it could be output to a file, or even printer. Moving the cursor around on a printer wouldn't do what you might want it to do.
As such, you could TRY printing backspace characters (\b) and see what happens, but better yet would be to obtain a terminal library - like ncurses; which will give you far better control of the cursor.
Upvotes: 1