Updating one line, instead of new lines

Hey i am trying to get a single long double to update when i add something to it. It works, but not how i want it to work. I am using cout in a while loop to update it and i was hoping for a way to update the same line in the command window, instead of it adding a new line underneath all the time.
Any help is appreciated.
Thanks.
The only time you'll get a new line is if you make one yourself with endl /*or*/ '\n', or if you're asking for user input. There's no way to get around the user input problem as far as I know, but if you're just incrementing the double then you could do something like:

1
2
3
4
5
6
7
double Cash;

for(int x = 0; x < NumOfIterations; x++)
{
	cout << Cash << ' ';
	Cash += Pay;
}


And it should display on one line with a space between each value.
I am pretty sure there is a way to delete the old both inputs from users and the cout's, as I think my teacher did it, I just dont know how.
You could try using \r and \b. The first moves the cursor back to the start of the line, the latter back one character. Neither erase the exisiting text, so you've got to supply enough chars to erase all the old ones.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <cstdio>
using namespace std;

int main()
{
	printf("hello");
	printf("\rbye");
	printf("\n\n");

	printf("hello");
	printf("\b\b\b\bu");
	printf("\n\n");

	return 0;
}


byelo

hullo

Last edited on
Thanks andywestken, but are there any way to go back up to the first line, almost as in starting the program over, but without it starting over of course?
There is no generic C or C++ mechanism, but there are platform specific ways.

For Windows, see:
How To Performing Clear Screen (CLS) in a Console Application
http://support.microsoft.com/kb/99261

For a *nix terminal that supports ANSI escape sequences, you just need to print the string "\x1B[2J". See:
ANSI escape code
http://en.wikipedia.org/wiki/ANSI_escape_code
Cool thanks
Topic archived. No new replies allowed.