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:
- '|' represents the cursor position