Alarm Clock Programming

(sorry if its in the wrong spot)

This is supposed to play a file (That I can do)
Save a file (That I can do)
Load a file (That I can do)
Get system time (That i can't do)
Basically what's supposed to happen is if system time = wished to be woken time then play an mp3 file or something...
Problem is I can't get the time in a variable, I figured a string would be best to avoid am/pm issues... but now i just want it to work.. i've spent maybe almost 2 hours... and its frustrating me.. i've tried everything i can think of... and google too. I'm on XP.. using Dev CPP

Heres the code:

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
//Alarm Clock By: Johnathan Brown

#include <iostream>
#include <fstream> //Might use a load previous time feature.. not sure yet
#include <string>
    

int main() {
using namespace std;
    
    //Variables
    
    int choice = 0;
    string current;
    string wake;

//I like my name.. lol.. so I put it in


    cout << "*************Johnny's Alarm Clock**********" << endl;
    cout << "Version 1.0" << endl;
    
    //MENU
    
 while (choice < 1 || choice > 3)
 {   
    cout << "1) Set a time to wake up to" << endl;
    cout << "2) Retrieve last used time to wake up to" << endl;
    cout << "3) Exit" << endl;
    cout << "What option would you like: ";
    cin >> choice;    

    switch (choice)
    {
    
    /*Here lies the problem.. I have no errors and its not complete yet
    but I can't get it to get the system time... and i've tried checking to
    see if it has it and it doens't.. what I need is the system time in
    a type of variable I thought i would need a string to avoid am/pm issues
    but this isn't working out..
    */
    case 1:
         system("cls");
          cout << "Be sure to set your windows time before doing this" << endl;
          system("pause");
          system("cls");
          cout << "The format is HH:MM am/pm for example 07:00 am" << endl;
          cout << "What time would you like to wake up? ";
          getline(cin, wake); // <-- UPDATED with "firedraco's" advice
          system("pause");
          system("time /t");
          getline (cin, current);
          system("cls");
          cout << "Current time is " << current;
          cout << "Wake up time is " << wake;
          system("pause");
          break;
          
          // DONT WORRY BOUT THIS PART YET
    case 2:
         system("cls");
          cout << "dont worry";
          break;
          //DONT WORRY ABOUT THE ABOVE YET
          
    case 3:
          cout << "Ok, Thank you!!!"; //Exit
          system("pause");
          return 0;
          break;
    default:
            cout << "That is not a number between one and 3" << endl;
            system("pause"); //In case number is outta range
            system("cls");
            break;
}
}
system("pause"); //End of program
return 0;
}
Last edited on
For getting strings:
http://www.cplusplus.com/forum/articles/6046/

The way you have it set up it is prompting the user for the time, (not correctly though, but the above will fix that), and next it gets data from cin into a string called current (which is the correct way), but since it is getting from cin you will need to have the user input the current time...

If you want to get the time form the system...it's actually a lot harder then (I think) it should be...go look up tm_struct and the related stuff.
The alarm time (cin will stop at the first whitespace).
I've updated the program according to your first post (strings).. I still can't get system time in format HH:MM AM/PM... the tm_struct looks complicated.. is there an alternate.... because I think I can work a quicker console job with batch... or even in vb.net.. btw.. I've noticed with the CIN update... i now get this in CMD.. it doesn't give the chance for an input time to wake up at


The format is HH:MM am/pm for example 07:00 am
What time would you like to wake up? Press any key to continue . . .


maybe theirs a header I can use.. and i'm still a beginner at C++ programming... would it be possible to turn this into a function.. or a class so next time i want the time in a program i won't have to go through the whole tm_struct (which i currently don't understand)

although about the above quote.. it seems to work again remembering the wake variable if i do this
cin >> wake;
and then add an underscore to format so its this now HH:MM_am/pm
.. still can't get system time though
Last edited on
Yes, like I said, cin stops at the first whitespace it sees...that is why you use getline(cin, myString) instead...if it skipped the first whitespace, the data is still in the buffer when it gets to the next cin>> then skips through it because it just reads the data.
I think it'd be easier to input the time in 24 hours format.

I don't think getting the system time is hard at all:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <cstdio>
#include <ctime>

int main(){
	time_t s=time(0);
	tm t=*localtime(&s);
	printf("%04d-%02d-%02d %02d:%02d:%02d\n",
		t.tm_year+1900,
		t.tm_mon+1,
		t.tm_mday,

		t.tm_hour,
		t.tm_min,
		t.tm_sec);
	return 0;
}
I think I'm going to give this alarm clock a break for now. I thought it would be a basic thing for a C++ newb, but it's way more than I was asking for, I figure I could work on organizing my code into functions and classes, it is quite messy, but all this help won't go to waste, I'm not giving up just pausing. Thanks all. (When done source code for v1.0 will be posted for all to see)
Topic archived. No new replies allowed.