I am trying to make Loop with While and Switch ,it switches from True/False when I press F4, also I wanna to make it soo I can switch from True to False unlimited number of times, this is smth like toggle button .
If someone can say him where is mistake in my code , thanks
Your code doesn't really make sense. Maybe you meant something like this:
1 2 3 4 5 6 7 8
bool t = false;
while (true) {
if (GetAsyncKeyState(VK_F4) < 0)
t = !t;
system("cls"); // a very expensive way to clear the console
cout << (t ? "on" : "off") << '\n';
Sleep(200);
}
@Thomas1965, nice one. Try this C++ timing measure and see if it's different. I think C's clock function only measures time that the process itself takes, but in the case of the system() call we are firing up another process to clear the screen. So the method below might actually show an even longer time. But since our process is waiting for the other process maybe it won't show a longer time. I'm curious. :-)
Unless you can blink 40 times a second, that's true. I agree that it hardly matters and system("cls") may be a lot easier on Windows than all that code. But on Linux you can usually just say std::cout << "\x1b[2J". I believe that works on some versions of Windows, too. It just seems silly to have to fire up an external program to clear the console.
It just seems silly to have to fire up an external program to clear the console.
Yes I suppose, but new programmers don't think about that. They just know it works. So they use it.
As for OP's code, I notice no break in your switch statement. This will cause the default option to execute even if the case is 'on'. The default calls cls faster than you can blink, as we discovered. So you will never see cout on.
And what is bool t being used for? It seems to be false, always.