How to print only 'i' and not repeat

Hello!

I am writing some practice programs and I was just wondering, what should I do so that instead of getting an output like this:

1
2
3
4
5
6
Percent Complete: 1%
Percent Complete: 2%
Percent Complete: 3%
Percent Complete: 4%
Percent Complete: 5%
...


I instead just have one line (Percent Complete:) where only 'i' is updated ?

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


int main()

{
	for (int i = 0; i <= 100; i++)
	{
		cout << "Percent Complete: " << i << "%" << endl;


	}
	system("pause");
	return 0;
}
Last edited on
you would either have to do something like this:

1
2
3
4
5
6
7
int main() {
    for(int i = 1; i < 101; i++) {
        std::cout<< i <<"% done";
        sleep(1);
        system("clear"):
    }
}
or use ncurses
Instead of endl, user \r ro return the current position to the start of the line.

1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;

int main()
{	for (int i = 0; i <= 100; i++)
		cout << "Percent Complete: " << i << "%\r";
	cout << endl;
	system("pause");
	return 0;
} 

Thank you both very much, I managed to fix it! :)
Topic archived. No new replies allowed.