0

I am writing an alarm clock program in C++, and it works great for now, but I was wondering is there any way I can have the user select the sound they want to blare once it goes off.

If it helps, here is my code, as bloated as it may be:

#include <iostream>
#include <ctime>
#include <dos.h>
#include <time.h>
#include <stdio.h>
#include <Windows.h>
#include <Mmsystem.h>
#include <conio.h>
#include <unistd.h>
#pragma comment(lib, "winmm.lib")
using namespace std;

int main(){
    // Figure out how to let them choose song
    int backup;
    int hours=0;
    int minutes;
    int seconds;
    int remain;
    time_t current;
    struct tm * timeinfo;
    int main;
TOGGLE1:
    system("CLS");
    cout<<"Console Alarm Clock v 2.0 beta 1"<<endl<<"Brown Beard the Pirate ©2010"<<endl<<endl<<endl;
    cout<<"What time do you want to be woken up? (on the 24 hour clock)"<<endl;
    cin>>backup;
TOGGLE2:
    main = backup;
    system("CLS");
    cout<<"Console Alarm Clock v 2.0 beta 1"<<endl<<"Brown Beard the Pirate ©2010"<<endl<<endl<<endl;
    while (main>60){
          hours++;
          main = main -100;
    }
    if (main<0){
                cout<<"Invalid time!"<<endl;
                goto TOGGLE1;
    }
    if (backup>2400){
                     cout<<"Invalid time!"<<endl;
                     goto TOGGLE1;
    }
    if (backup>1259){
                   cout<<"Clock set for "<<backup-1200<<" PM"<<endl;
    }else{ if(backup>1300 && backup<1159){
                        cout<<"Clock set for "<<backup<<" PM"<<endl;
    }else{
          cout<<"Clock set for "<<backup<<" AM"<<endl;
          }
    }
    minutes = hours*60+main;
    seconds = minutes * 60;
    time (&current);
    timeinfo = localtime (&current);
    int hour = timeinfo->tm_hour;
    int min = timeinfo->tm_min;
    int sec = timeinfo->tm_sec;
    int timeleft = hour*3600+min*60+sec;
    remain = seconds - timeleft;
    while (remain !=0){
          time (&current);
          timeinfo = localtime (&current);
          hour = timeinfo->tm_hour;
          min = timeinfo->tm_min;
          sec = timeinfo->tm_sec;
          timeleft = hour*3600+min*60+sec;
          remain = seconds - timeleft;
          Sleep(1);
    }
    //Implement chosen song instead of just alarm.wav
    PlaySound("alarm.wav",NULL,SND_FILENAME|SND_LOOP|SND_ASYNC);
    string answer("");
ASK1:
    cout<<"Do you want to reset the clock?"<<endl;
    cin>>answer;
    if (answer.compare("Yes") == 0||answer.compare("yes")==0||answer.compare("Y")==0||answer.compare("y")==0){
ASK2:
                              PlaySound(NULL, 0, 0);
                              cout<<"Same time?"<<endl;
                              cin>>answer;
                              if (answer.compare("Yes") == 0||answer.compare("yes")==0||answer.compare("Y")==0||answer.compare("y")==0){
                                                        goto TOGGLE2;
                              }else {if (answer.compare("No") == 0||answer.compare("no")==0||answer.compare("N")==0||answer.compare("n")==0){
                                    goto TOGGLE1;
                              }else {
                                    cout<<"Invalid answer!"<<endl;
                                    goto ASK2;
                                    }
                                    }
                              }else {if (answer.compare("No") == 0||answer.compare("no")==0||answer.compare("N")==0||answer.compare("n")==0){
                                    PlaySound(NULL, 0, 0);
                                    int y;
                                    cin>>y;
                                    return 0;
                              }else{
                                    cout<<"Invalid answer!"<<endl;
                                    goto ASK1;
                                   }
                              }
}

Edited by BrownBeard: forgot code

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Dingbats
0

Hello,

you haven't indicated that you've sorted this out so...an idea...

Introduce a variable (as you indicated). Then introduce a menu at line 71 which lists out which music is available, and then use a switch statement on the return value (presumably of type int) to play the chosen piece of music?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.