Boolean

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
bool t = false;
int wmain()
{
	while (TRUE)
	{
		switch (GetAsyncKeyState(VK_F4)) {		
		case 'on':
			system("cls");
			cout << "on" << endl;
			t = false;
		default:
			system("cls");
			cout << "off" << endl;
			t = false;
		}
		Sleep(100);	
	}
	return 1;
}
Last edited on
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);
    }

Last edited on
system("cls"); // a very expensive way to clear the console

It is indeed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <ctime>
#include <Windows.h>

using namespace std;

// based on MSDN
void ClearScreen()
{
  COORD coordScreen = { 0, 0 };    // home for the cursor 
  DWORD cCharsWritten;
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  DWORD dwConSize;
  HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
 
  GetConsoleScreenBufferInfo(hConsole, &csbi);
  dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
  FillConsoleOutputCharacter(hConsole, ' ',  dwConSize, coordScreen, 
    &cCharsWritten);

  GetConsoleScreenBufferInfo(hConsole, &csbi);
  FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize,        
    coordScreen, &cCharsWritten);

  SetConsoleCursorPosition(hConsole, coordScreen);
}

int main()
{
  clock_t start = clock();
  system("cls");
  clock_t stop = clock();

  clock_t start2 = clock();
  ClearScreen();
  clock_t stop2 = clock();
  cout << "Time for system(\"cls\"): " << double(stop - start) / CLK_TCK * 1000 << "ms\n\n";
  cout << "Time for ClearScreen: " << double(stop2 - start2) / CLK_TCK * 1000 << "ms\n\n";
}


Time for system("cls"): 25ms
Time for ClearScreen: 1ms
Last edited on
@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. :-)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <chrono>
#include <cstdlib>

// ClearScreen function here...

long f() {
    auto start = std::chrono::steady_clock::now();

    system("cls");

    auto end = std::chrono::steady_clock::now();
    auto diff = std::chrono::duration_cast<
                    std::chrono::nanoseconds>(end - start);
    return diff.count();
}

long g() {
    auto start = std::chrono::steady_clock::now();

    ClearScreen();

    auto end = std::chrono::steady_clock::now();
    auto diff = std::chrono::duration_cast<
                    std::chrono::nanoseconds>(end - start);
    return diff.count();
}

int main() {
    auto fres = f();
    auto gres = g();
    std::cout << fres << "ns\n";
    std::cout << gres << "ns\n";
}


Last edited on
25 ms... is equal to .025 seconds. It takes me longer than that to blink, i think.
It takes me longer than that to blink

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.
Last edited on
@tpb,
your code output:

123033847ns
278045ns


But on Linux you can usually just say std::cout << "\x1b[2J". I believe that works on some versions of Windows
Doesn't work on Windows 7, shows
←[2J

Thanks for those anwears,I appreciate that very much ,this forum is full of great people ,you are awesome ,much love :)
@Thomas1965,So the system() call took 442 times as long!

I think there's something you can add to windows so the VT-100 terminal codes work. Also, I've heard that they work on windows 10.
Topic archived. No new replies allowed.