OK I have a simple app that generates 2 integers and asks the user to sum them and enter the result as input. I am checking that the input is an integer. If it’s not I want to rewrite the whole line instead of just outputing the same line. For example:
prompt:
2 + 2 =
user enters this and presses ENTER:
2 + 2 = s
I want to have this in the terminal:
2 + 2 =
instead of this:
2 + 2 = s
2 + 2 =
I understand that C++ is very abstract and has no notion of a terminal or where the output goes. I’m using linux. Any way I can do the above?
You should be able to do this directly in most terminals by printing out the right control characters. For example, if you print a carriage return instead of a new line the cursor may be taken back to the beginning of the line to write over what’s been printed on that line.
However this is dependent on how your terminal deals with these characters, and so you might want a library that provides a standard interface for these terminal specific features, like ncurses.