What you want to do isn't possible with just scanf
and printf
and without using arrays. You'd need to take over the character-by-character input system, using something like the readline
library or the terminfo
library.
The fundamental problem is that scanf(' %f', ...)
will scan until it reads a floating point value, discarding all whitespace until then. "Whitespace" here not only means spaces and tabs but line ends as well! So the user can type RETURN a few times, the number, RETURN a few more, the second number, and RETURN before your code even gets to it. Your code would then need to obliterate all this malformatted input, work its way back up to the proper line, which may well have scrolled away by then, and print the properly formatted values.
Thus you need something better than scanf
which will be able to block RETURN (and ENTER and long lines) from scrolling the screen, and you need terminfo
or equivalent to get the control sequences needed to do things like "erase this line" and "move cursor to this point".
scanf
is no substitute for a robust parser, unfortunately. It's designed around reading input files and isn't very smart about terminal input.