Clock Program

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2008
Posts: 100
Reputation: Dewey1040 is an unknown quantity at this point 
Solved Threads: 3
Dewey1040 Dewey1040 is offline Offline
Junior Poster

Clock Program

 
1
  #1
Nov 3rd, 2009
I am supposed to create a program like this, with separate clock.cpp, clock.h, and main.cpp files. The program is supposed to do these tasks: I have started the clock.cpp and dont quite know where to go for some of the tasks, and I don't quite know how to use the time.h library

* Setting the Clock's current time by specifying hours, minutes as arguments
* Setting the Clock's current time by reading the time from the operating
system. Use the time() and localtime() calls for this: see "man 2 time", "man 3
localtime". ( Any suggestions on how to go about doing this? )
* Being configured as either a 24-hour or am/pm clock (picture a switch on the
back of a clock that changes its display mode)
* Printing the Clock's current time
* Getting the Clocks' current time (returning hours, minutes as output
parameters -- this is different than printing the current time) (Any suggestions on how to do this? )
* Comparing the time represented in two Clocks using operator<. ( havent attempted this but I don't see it as being so difficult )

  1. #include <time.h>
  2. #include <iostream>
  3. #include <sstream>
  4. using namespace std;
  5.  
  6. class Clock{
  7.  
  8. private:
  9.  
  10.  
  11. public:
  12. typedef struct{
  13. int hours;
  14. int minutes;
  15. bool military;
  16. bool ampm;
  17. } time;
  18.  
  19. time setTime( time A ){
  20. cout << "Enter Hours:";
  21. cin >> A.hours;
  22. cout << "\nEnter Minutes:";
  23. cin >> A.minutes;
  24. cout << "\nEnter 1 for a twelve hour clock, 0 for 24";
  25. cin >> A.military;
  26. if( A.military ){
  27. if( A.hours > 12 ){
  28. A.hours = A.hours - 12;
  29. A.ampm = 1;
  30. }
  31. }
  32. }
  33. void printTime( time A ){
  34. cout << "The current time is:" << A.hours << ":" << A.minutes << endl;
  35. }
  36.  
  37. }

To be honest, i havent created the main program yet, but will do that shortly and will have more questions, Im just seeing what kind of input i get for this program so far.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,620
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1492
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning
 
-7
  #2
Nov 3rd, 2009
The functions in time.h aren't at all difficult to use -- you just need to read about the different functions.

time() -- returns the current time in seconds since 1970. It returns the time in an unsigned int (size_t) variable. When you have to get the current time this is the first function you want to call.

localtime() -- takes the return from time() and converts it to a struct tm structure.
  1. time_t now = time(0); // get time in seconds
  2. struct tm* tm = localtime(&now);

From there you will easily see how to extract the hour and minutes from the struct tm structure. If you know how to use structures and pointers then this will be a snap for you.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 100
Reputation: Dewey1040 is an unknown quantity at this point 
Solved Threads: 3
Dewey1040 Dewey1040 is offline Offline
Junior Poster
 
0
  #3
Nov 5th, 2009
so i basically re did the files, and I'm getting the error time does not name a type... heres my code for the .h and .cpp

Clock.cpp:
  1. #include <time.h>
  2. #include <iostream>
  3.  
  4. #include "Clock.h"
  5.  
  6. using namespace std;
  7.  
  8. time::time(){
  9. hours = minutes = 0;
  10. }
  11.  
  12. time::time( int hours2, int minutes2 ){
  13.  
  14. hours = hours2;
  15. minutes = minutes2;
  16. }
  17.  
  18. int time::getHours() const{
  19.  
  20. return hours;
  21. }
  22.  
  23. int time::getMinutes() const{
  24.  
  25. return minutes;
  26. }
  27.  
  28. void time::setHours(const int hours2){
  29.  
  30. hours = hours2;
  31. }
  32.  
  33. void time::setMinutes( const int minutes2){
  34.  
  35. minutes = minutes2;
  36. }
  37.  
  38. time time::operator+(const time &t1) const{ //Error points to this line
  39.  
  40. time sum;
  41.  
  42. sum.minutes = minutes + t1.minutes;
  43. sum.hours = hours + t1.hours + sum.minutes/60;
  44. sum.minutes %= 60;
  45.  
  46. return sum;
  47. }
  48.  
  49. void time::show() const{
  50. cout<<hours<<":"<<minutes<<endl;
  51. }

Clock.h
  1. #include <time.h>
  2. #include <iostream>
  3. using namespace std;
  4. #ifndef _Clock_H_
  5. #define _Clock_H_
  6.  
  7. class time{
  8.  
  9. private:
  10. int hours, minutes;
  11.  
  12. public:
  13.  
  14. time();
  15. time( int hours2, int minutes2 );
  16.  
  17. int getHours() const;
  18. int getMinutes() const;
  19. void setHours( const int hours2 );
  20. void setMinutes( const int minutes2 );
  21. time operator+( const time &t1 ) const;
  22. void show() const;
  23. };
  24.  
  25. #endif
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 100
Reputation: Dewey1040 is an unknown quantity at this point 
Solved Threads: 3
Dewey1040 Dewey1040 is offline Offline
Junior Poster
 
-1
  #4
Nov 5th, 2009
bump
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 100
Reputation: Dewey1040 is an unknown quantity at this point 
Solved Threads: 3
Dewey1040 Dewey1040 is offline Offline
Junior Poster
 
0
  #5
Nov 6th, 2009
so after working on this program for a while i think im getting somewhere, but im getting the error message:

Clock.h:12: error: expected constructor, destructor, or type conversion before ‘using’

any idea why i would be getting an error for
  1. using namespace std;
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,452
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 250
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c
 
0
  #6
Nov 6th, 2009
time is a reserved identifier.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC