Hello, I'm still a huge beginner in C++ and while i spent hours trying to find a solution to this problem i came to this forum pretty often and I thought it'd be a good idea if I try to ask about my problem. (If you notice anything that can be done better I'd be more than happy to hear an criticism, advice, or lesson about coding). I just wanted to try a little project on my own outside of my C++ class and I wanted to make a timer for myself (since i always end up losing my sense of time on the computer haha), Everything was going fine until i found the urge to make a Snooze and shutdown available while my program continuously plays a beep (Sort of like an alarm clock). Obviously Im stuck at being able to input something while the beep is going on. I don't even know if its possible since If i do cin>> in the loop itll only beep once before it waits for input and if i dont itll just never stop looping. Line 40 and down is me trying to use getch() (I read up about inputting without the prompting of enter, so i was hoping it would somehow work) but it doesnt work the way I want it to.
Edit: i use dev-cpp btw :P (If theres a better program/compiler out there id also be glad to hear about that.)

#include <iostream>	
#include <time.h>
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
 
using namespace std;
	
int main()                            
{
 int num; //number being inputted by the user for the time.
 int input; //number inputted by the user in order to make his selection.
 
 cout<<"Please choose what you would like your countdown in.\n"; //Whether the user wants their time in minutes or seconds.
 cout<<"1.Seconds\n";
 cout<<"2.Minutes\n";
 cin>>input; //The user inputs his selection for the countdown.
 Beep(1450,300);
 system ( "cls" );

      switch (input){
         case 1: //The user has chosen his countdown in seconds (1000 ms countdown)
         cout<<"Please input the number of seconds to initiate the countdown.\n"; //Input by the user
         cin>>num;
         system ( "cls" );
         if (num>0) //If the user follows the instructions correctly.
         {
              while (num>0) //Literal countdown sequence
              {
              cout<<num<<"\n";  
              num--;   
              Beep(1450,100);
              Sleep(900);
              system ( "cls" );
              }
                  while (num==0) // When the countdown reaches 0
                  {
                  cout<<"*ALARM*\n";
                  Beep(1450,100);
                  num = getch();
                  if (num!=0)
                     {cout<<"Shutting down alarm...";
                      Sleep(3000);
                      return 0;
                      }

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.

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.

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);
    }
}

I tested out the code and I have to say I very much like the way it works :). Ill read up more about signals around and try to incorporate it. Thank you very much for your help. Ill post if i have any success after I have a chance to research signals a bit.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.