Looks like your program got cut off, but I think the general gist is this. Programs like this are sequential. One thing happens at a time. You are either beeping, calculating, waiting for input, display, calculating, whatever. Two things can't happen at the same time in most programs.
If you want to be able to beep and accept input at the same time, you need a separate process and/or separate threads. That can get complex. I am not too familiar with the "system" command. It's generally frowned on, but it may be a blunt instrument that can do the job in your case. I don't know.
But the right way would be to have a process/thread (there are pros and cons to each) that is spawned when the alarm goes off and which is run separately. When snooze is hit, you kill that beep process. Looks like you're in Windows. I know nothing about how to create threads/processes in Windows C++, and to be fair, not much more on ho to do it in Linux.
But I think that what you are trying to do cannot be done in one process containing one thread.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
Yeah I only put the part of the code dealing with Seconds since thats the part im trying to refine, i plan on working on minutes and hours at a later time. and referring to the multiple threads i also saw that it was way to complicated to try to incorporate (like i said im still a beginner). Referring the the system command, i was trying to find a good way to clear screen just for aesthetic purposes of the program, if it counted down in seconds it would clear it every time it outputted which i thought looked nicer, if you know a better way to clear screen id be happy to hear it. Ill look into the signal site when i have time, thank you.
Here's a functioning program using signals that you can use as a model. Press Ctrl-C for Snooze. After five alarms, it exits. If you override the traditional Ctrl-C like I have, you should make sure that the program doesn't go on forever (hence the loop of five). You can make this program much more elaborate, but this is the basic idea.
#include <csignal>
#include <iostream>
#include <windows.h>
using std::cout;
void SnoozeButtonPressed (int sig);
int timer;
int numAlarms;
int main()
{
// override normal Ctrl-C signal
if(signal(SIGINT, SnoozeButtonPressed) == SIG_ERR)
{
cout << "Problem setting up signal.\n";
exit (1);
}
cout << "Press Ctrl-C for snooze button. Program will halt after alarm goes off 5 times.\n";
numAlarms = 0;
timer = 5;
while (numAlarms < 5)
{
while (timer > 0)
{
cout << "Alarm has gone off " << numAlarms << " times. Seconds till alarm : " << timer << "\n";
timer--;
Sleep (1000);
}
while (timer <= 0)
{
cout << "Beep!\n";
Beep (100, 1000);
Sleep (1000);
}
numAlarms++;
}
return 0;
}
void SnoozeButtonPressed (int sig)
{
cout << "Snooze Button pressed! Five seconds added to timer.\n";
timer = timer + 5;
// reset signal
if(signal(SIGINT, SnoozeButtonPressed) == SIG_ERR)
{
cout << "Problem setting up signal. Exiting\n";
exit (1);
}
}
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711