| | |
Clock Program
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2008
Posts: 96
Reputation:
Solved Threads: 3
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 )
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.
* 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)
#include <time.h> #include <iostream> #include <sstream> using namespace std; class Clock{ private: public: typedef struct{ int hours; int minutes; bool military; bool ampm; } time; time setTime( time A ){ cout << "Enter Hours:"; cin >> A.hours; cout << "\nEnter Minutes:"; cin >> A.minutes; cout << "\nEnter 1 for a twelve hour clock, 0 for 24"; cin >> A.military; if( A.military ){ if( A.hours > 12 ){ A.hours = A.hours - 12; A.ampm = 1; } } } void printTime( time A ){ cout << "The current time is:" << A.hours << ":" << A.minutes << endl; } }
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.
-7
#2 25 Days Ago
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.
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.
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)
time_t now = time(0); // get time in seconds 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.
•
•
Join Date: Dec 2008
Posts: 96
Reputation:
Solved Threads: 3
0
#3 23 Days Ago
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:
Clock.h
Clock.cpp:
C++ Syntax (Toggle Plain Text)
#include <time.h> #include <iostream> #include "Clock.h" using namespace std; time::time(){ hours = minutes = 0; } time::time( int hours2, int minutes2 ){ hours = hours2; minutes = minutes2; } int time::getHours() const{ return hours; } int time::getMinutes() const{ return minutes; } void time::setHours(const int hours2){ hours = hours2; } void time::setMinutes( const int minutes2){ minutes = minutes2; } time time::operator+(const time &t1) const{ //Error points to this line time sum; sum.minutes = minutes + t1.minutes; sum.hours = hours + t1.hours + sum.minutes/60; sum.minutes %= 60; return sum; } void time::show() const{ cout<<hours<<":"<<minutes<<endl; }
Clock.h
c++ Syntax (Toggle Plain Text)
#include <time.h> #include <iostream> using namespace std; #ifndef _Clock_H_ #define _Clock_H_ class time{ private: int hours, minutes; public: time(); time( int hours2, int minutes2 ); int getHours() const; int getMinutes() const; void setHours( const int hours2 ); void setMinutes( const int minutes2 ); time operator+( const time &t1 ) const; void show() const; }; #endif
•
•
Join Date: Dec 2008
Posts: 96
Reputation:
Solved Threads: 3
0
#5 22 Days Ago
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
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)
using namespace std;
![]() |
Similar Threads
- digital clock (C++)
- Best way to do a clock program (Java)
- c++ clock? (C++)
- Clock Format (Visual Basic 4 / 5 / 6)
- "Server Busy" Switch To or Retry (Viruses, Spyware and other Nasties)
- Updating Controls (C++)
- Persistent spyware problems, HJT log included. (Viruses, Spyware and other Nasties)
- hijackthis log...I need help please (Viruses, Spyware and other Nasties)
Other Threads in the C++ Forum
- Previous Thread: A (*) star algorithm too slow
- Next Thread: Viewing a buffer when using std::string advice.
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






