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.
/*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
usingnamespace 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
}
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!?!";
}
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 :(