Alarm Clock (V1.0)

Feb 27, 2009 at 6:24am
A while ago.. (December 2008) I started an alarm clock.

Well today, I found enough time to finish, so I'll just post the code here. It's still in development but its 1:23 eastern, and I'm tired, so this is only version 1.0 feel free to add more if you want, I would appreciate the credit.

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*Alarm Clock Mp3 Player By JOHNATHAN BROWN (Legend28469)
  Set a time.. when the System Time is the same as the time set
  play an mp3
  
  Date: Thursday, February 27, 2009 -- 12:00 am (EST Time)
  */

#include <iostream> //Input output
#include <ctime> //To get the system time
#include <fstream> //If wishing to load a previous wake time
#include <string> //Hold the filename info

using namespace std;

//Global.. and Local
time_t s=time(0); //Getting ready for time
tm t=*localtime(&s); //Getting time put into Variable t

bool DisplayIntro() {
    cout << "**********************************************\n";
    cout << "******   LEGEND28469's Alarm Clock    ********\n";
    cout << "******************Version 1.0*****************\n";
    cout << "Johnathan Brown Productions..\n\n";
    //Not much to explain.. self explanatory
    cout << "Instructions for use:\nPick an option in the menu.. Have Fun!!\n";
    system("PAUSE"); //Wait for an enter
    system("CLS"); //Clear screen
}

    bool SetTime() { //Hold time in memory (to wake up)
        system("CLS"); //Clear system screen
        cout << "The Current Time is: " << t.tm_hour << ":" << t.tm_min;
        cout << "\nIf this time is incorrect then please set your system time\n";
        system("PAUSE"); //Give time to read
        
        int hour; //HOLD the hour somewhere
        int min; //HOLD the minute somewhere
        string mp3; //Hold the name of the mp3 in here
        
        cout << "\n\n\n"; //Clear a few lines
        
        cout << "What hour would you like to wake up at [24 hr format]: ";
        cin >> hour; //Put input into hour
        
        cout << "\nWhat minute would you like to wake up at: ";
        cin >> min; //Put input into min
         
        
        cout << "\nPlease type the FULL path of the media file to open\nFor Example:";
        cout << " C:\\cannon.mp3\n";
        cout << "It must be in C: and no spaces allowed\n";
        cout << "Please type it here --> ";
        cin >> mp3;
        
        cout << "\nInitializing Alarm Clock...\n";
        cout << "Alarm Clock in progress... to wake you up at " << hour << ":" << min;
        
        //Mp3 Info or whatever file type
        
        loop: //Keep getting the time until everything adds up...
        
        time_t s=time(0); //Getting ready for time
        tm t=*localtime(&s); //Getting time put into Variable t
        
    if (hour != t.tm_hour)
    {
        goto loop;
    }
        
    
    if (min != t.tm_min)
    {
        goto loop;
    }
    
    cout << "\nShowtime\n"; //Play the mp3
    
	system(mp3.c_str()); //Play mp3
    
    }

    bool Retrieve() {
     cout << "That feature hasn't been implemented yet.. sorry!!!\n";
     system("pause");
    } 

int DisplayMenu() {
    //Declare where the choice is going to go
    int choice = 0;
    
    /*While int choice is less than 1 (which it currently is),
    or greater than 3 (in case they choose an option off the list)
    Keep displaying the following
    */
    
    while (choice < 1 || choice > 3) {   
    cout << "1) Set a time to wake up to" << endl; //Handle things in SetTime
    cout << "2) Retrieve last used time to wake up to" << endl; //Handle things in Retrieve
    cout << "3) Exit" << endl; //Exit the Program
    cout << "What option would you like: ";
    cin >> choice;
    
    switch (choice)
    {
           case 1:
           SetTime();
           break;
           
           case 2:
           Retrieve();
           break;
           
           case 3:
           return 0;
           break;
           
           default:
            cout << "That is not a number between 1 and 3" << endl;
            system("pause"); //In case number is outta range
            system("cls");
            break;
    }
    }
}

int main(int argc, char *argv[]) {
    
    //Function Calls
    DisplayIntro(); //Get the System Time
    DisplayMenu(); //Display the menu.. and have the user make a choice
    
    cout << "Thank you for using Legend28469's Alarm Clock\n\n";
    system("PAUSE");
    return EXIT_SUCCESS; //Or return 0
}


Feb 28, 2009 at 9:28pm
http://www.cplusplus.com/forum/articles/6046/
You have no type checking on your cin >> statements. If you enter something that is not an expected type the program will crash.
Mar 2, 2009 at 9:47pm
lol Looks like i'm gonna wake up at 692:653 also zaita it didn't crash
type check is a good idea lol
Last edited on Mar 2, 2009 at 9:47pm
Mar 3, 2009 at 1:59am
Cute program, I love it, but...

That program is incompatible with Linux, or any other OS other than Windows. Sorry, but, system() really is evil when there is no support for other OSes.

Also, as stated before, there is no type checking for the cin >> statements. I believe there is already a link to the article on that. Or, you could put a try{} block around your inputs. In example with the try statement:

1
2
3
4
5
6
7
8
try
{
    cin >> choice;
}
catch (int e)
{
    cout << "Error number " << e << " sez: What are you trying to do, crash the program!?!";
}


But it is a useful program otherwise.
Last edited on Mar 3, 2009 at 1:59am
Mar 4, 2009 at 7:04am
Albatross:
Cute program, I love it, but...

That program is incompatible with Linux, or any other OS other than Windows. Sorry, but, system() really is evil when there is no support for other OSes.


I think he is using devcpp (i used it for a while but got tired of no debug), if you delete the pause, it should be ok. oh...nevermind, i just saw all the other ones too bad :(
Topic archived. No new replies allowed.