100 - 1 = 990, 990 - 1 = 980 while 120 - 1 = 119 and 119 - 1 = 118

closed account (z1CpDjzh)
This Code:
1
2
3
4
5
6
7
8
9
	for (int cntr = 100; cntr >= 0; cntr = cntr - 1)
	{
		std::cout << "\r\r\r" << cntr << std::flush;
		std::this_thread::sleep_for(std::chrono::seconds(1));
    }
	std::cout << "\r" << std::flush;

	std::cin.get();
	return 0;


Prints Out:

100
990
980
970
960
950
940
930


while if I make it:
1
2
3
4
5
6
7
8
9
	for (int cntr = 120; cntr >= 0; cntr = cntr - 1)
	{
		std::cout << "\r\r\r" << cntr << std::flush;
		std::this_thread::sleep_for(std::chrono::seconds(1));
    }
	std::cout << "\r" << std::flush;

	std::cin.get();
	return 0;

it prints out:
120
199
198
197
196

and so on.... util it reaches 100 cause that's when it does like the first code... why?
Last edited on
closed account (z1CpDjzh)
@NoXzema Whats the code for.... i dont need it.
closed account (z1CpDjzh)
@ne555 Yes Why does it go to 990 and start subtracting by 10s once it reaches 100?
@TheGentlmen

Your problem is in the 3'\r', which brings the cursor back to the beginning of the line, and a new number overwrites the previous one. Well, the number starts at 100, then the 99 overwrites the 1 and zero, leaving the final zero where it was. then the 98 overwrites the 99, still leaving the last zero. When you started the loop with 120, the 119 was able to overwrite the full previous number, until, of course, it reaches 99, then you have the same problem. To rectify it, try changing std::cout << "\r\r\r" << cntr << std::flush; to std::cout << "\r\r\r" << cntr << " " << std::flush;. (Added a space)
Last edited on
closed account (z1CpDjzh)
@whitenite1 I see...
This code runs perfectly:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#pragma comment(lib, "NewTDL.lib")
#include "NewTDL.hpp"

//NewTech Suit Launcher: NewTech Suit Launcher

int MEntry()
{	 
	for (int cntr1 = 100; cntr1 >= 0; cntr1 = cntr1--)
	{
		std::cout << "\r" << cntr1 << (cntr1 == 99 || cntr1 == 9 ? " " : "") << "\r"; 
		std::this_thread::sleep_for(std::chrono::milliseconds(0250));
		
    }std::cout << "\r";

	std::cin.get();
	return 0;
}
@TheGentlmen

Glad it now works. And I like your solution for printing the space. My way would probably mean using 4 '\r''s to counteract the space added. Didn't really check it.
Or just... use a newline...
Topic archived. No new replies allowed.