944,150 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1363
  • C++ RSS
Nov 3rd, 2009
1

Clock Program

Expand Post »
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 )

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 17
Solved Threads: 3
Junior Poster
Dewey1040 is offline Offline
126 posts
since Dec 2008
Nov 3rd, 2009
-7
Re: Clock Program
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.
C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Nov 5th, 2009
0
Re: Clock Program
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:
C++ Syntax (Toggle Plain Text)
  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
c++ Syntax (Toggle Plain Text)
  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
Reputation Points: 17
Solved Threads: 3
Junior Poster
Dewey1040 is offline Offline
126 posts
since Dec 2008
Nov 5th, 2009
-1
Re: Clock Program
bump
Reputation Points: 17
Solved Threads: 3
Junior Poster
Dewey1040 is offline Offline
126 posts
since Dec 2008
Nov 6th, 2009
0
Re: Clock Program
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
c++ Syntax (Toggle Plain Text)
  1. using namespace std;
Reputation Points: 17
Solved Threads: 3
Junior Poster
Dewey1040 is offline Offline
126 posts
since Dec 2008
Nov 6th, 2009
0
Re: Clock Program
time is a reserved identifier.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Input file stream -- endless loop
Next Thread in C++ Forum Timeline: Viewing a buffer when using std::string advice.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC