Void: Execution problems

I'm not new to C++, but i figured this would be the proper forum to ask a question like this in. I have not used functions declared as void until now because i had no need for them. But recently, i wrote a timer app and when the timer is done, it beeps in incriments while showing a "flashing" 'TIMER DONE!' message for the user. The user can then press any of the 4 buttons to turn the alert off: Space, Backspace, escape, or enter. Because a while loop is required for a continual alert, i used GetAsyncKeyState() to recieve the input. The problem is that GetAsyncKeyState() "gets" the key regardless of whether the program is the domonant one at the time. I could be browsing the web and turn off the alarm even though the program is minimized. Also, if you pressed the Enter key or any other deactivation key before starting the alarm, it would create the effect of the alarm not sounding. It would think you pressed the button and turn off immediately, when the alarm sounds, creating the effect of no alarm sounding at all. So, naturally, i decided to experiment with a different approach: What if we called a void function to execute the alarm, and then have a loop to get input with _getch(). Since _getch() requires a pause it is necessary that the function being called does not return a value.

Here is the sample code (t_done() is called first, becaude we dont want to call it more than once):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void t_done();
    while(x == 0)
    {
        clrg();
        ch = _getch();
        if((ch == 0x0d) || (ch == 0x08) || (ch == 0x1b) || (ch == 0x20))
        {
            cls();
            clrg();
            break;
        }
    }
    return 0;
}


t_done() code:

1
2
3
4
5
6
7
8
9
10
11
12
void t_done()
{
    int x = 0;
    while(x == 0)
    {
        cls();
        cout<< "TIMER DONE!";
        Beep(1500, 500);
        cls();
        Sleep(500);
    }
}


clrg() code:

1
2
3
4
5
6
int clrg()
{
    Sleep(100);
    while(_kbhit()) _getch();
    return 0;
}


cls() code:

1
2
3
4
5
int cls()
{
    system("CLS");
    return 0;
}


i have obvserved different behavior each time i execute it. The first time it works as expected. The second time it doesnt sound the alarm and blanks the screen while waiting for input (as if skipping the t_done() all together). After that the same thing... My reason for this approach is simple: Because void functions do not return a value, the program will not wait for the function to finish before executing the remaining instructions. When I call t_done t_done(); though, it acts like any other function. The only thing that has yeilded promising results was calling it void t_done();. Unfortunatly, it yeilds different results each time and still ignores the clrg() when accepting input. During tests i would press [enter] during a 5 second countdown and it would act as though i had pressed it while the alarm was sounding, which indicates it did not clear the _getch() stream at all. I would appreciate any help I can get! Thank you ahead of time.
Last edited on
void t_done(); is not a function call but a function definition
t_done(); is a function call.

Because void functions do not return a value, the program will not wait for the function to finish before executing the remaining instructions.
Nope, it doesn't work like that. They work as any other function.
Simply putting void will not give you multithread.

¿why are you calling _getc() so many times ?
Your loop should be something like
1
2
while( keep_running() )
  show_alert();
I want the user to have to press the button to stop the alarm (hence _getch()). Also, it is only called once. If the button pressed corresponds to the buttons that are meant to be pressed (in otherwords, you cant just press any button on the keyboard), it will _getch() again.

Also, could you explain your solution to me, that would be helpful.
Last edited on
So, how can i make the alert run while accepting input like _getch()?
The idea was something like (not tested)
1
2
3
4
5
6
7
8
9
10
bool keep_running(){
  if( not _kbhit() ) return true;
  int c = _getch();
  return not ((ch == 0x0d) || (ch == 0x08) || (ch == 0x1b) || (ch == 0x20));
}

void show_alert(){
    cout<< "TIMER DONE!";
    Beep(1500, 500);
}
interesting. I appreciate your help. I however, have abandoned that approach and used a different method: I wrote a program that ran a never-ending beep (no window) and when you press a button, it will end the program. I will, however find your methodology quite usful for the input while the timer is running. Right now, if you press excape, GetAsyncKeyState will stop the timer in mid-countdown (because there has to be some way top stop it, lol) and i wanted to confine input to the program and not anything. For instance, i dont want to be watching YouTube, press escape to exit full screen, and stop my timer.

Thank you very much for your help. Any others who wish to give advice may do so. I will appreciate your help! There is no end to what one can learn.
I tryed your suggestion. It worked! Very nice sir. I decided to create a new function for better execution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int bp(char key)
{
    char ch;
    if(!(_kbhit()))
    {
        return 0;
    }
    ch = _getch();
    if(ch == key)
    {
        return 1;
    }
    return 2;
}


If bp returns a 1, then the correct key has been pressed to activate it. If it returns a 0, then nothing will happen. If a 2 is returned, there is somthing wrong. This will certainly help in other program i have written.
Last edited on
Topic archived. No new replies allowed.