Trying to get this code to work. What the current issue I have is how do I get the now object to get the current time. I also want to know how to use the bool variable so that I can get it to say am or not. Any help with this is appreciated.
Thanks.
(Answers are not as valuable as successful teaching. Don't give me the answer. Help me understand what would make it work and how.)
// This lab exercise is to practice defining a class with methods.
***********************************************************************************
#include <iostream>
#include <string>
#include <iomanip>
usingnamespace std;
// ***********************************************************************************
// In this lab, you'll be defining a simple time class with a constructor
// and a method, in addition to data members.
//
// ***********************************************************************************
// Part A.
// declare a class called Time with
// 1) two int data members called hour and min
// 2) a boolean data member called am
// 3) a constructor that takes no arguments
// (remember that a constructor has the same name as the class, and no return value)
// 4) a Print method that takes no arguments and returns void
// DON'T FORGET THAT YOUR CLASS DEFINITION ENDS WITH ;
class Time
{
public:
int hour, min;
bool am;
Time();
void Print();
};
// Part B.
// define the two methods you declared in your class
// remember that their full name are: Time::Time and Time::Print
// 1) Time::Time should set hour and min to 0 and am to true
Time::Time()
{
hour = 0;
min = 0;
am = true;
}
void Time::Print()
{
cout << hour << ":";
cout << setw(2) << setfill( '0' ) << min;
}
// Part C.
// for contrast, declare a global function print that takes a Time
// object and prints it, returning nothing
void print(Time t1)
{
cout << t1.hour << ":" << t1.min;
}
int main( )
{
// Part D.
// 1) declare a Time object called now
// 2) set the hour, min and am members to the current time
// 3) call the class print method to print the time
// 4) call the global print function to print it again
Time now;
now.Print();
print(now);
return 0;
}
So is there no way I can use some sort of function to read what my computer clock says and manipulate that into my time object? Seems like a bit of work trying to count down to what time it is exactly from what looks like an 11 digit number.
I tried using the time libraries but it seems to have some bugs that aren't expected. I figured I'd just take the time input from the user. It seems to work flawlessly like that. Thanks for the help. If anyone can figure out in their own time how to get the code to take the seconds and get the current time of day off of that please post or pm me so I can understand it.
If anyone can figure out in their own time how to get the code to take the seconds and get the current time of day off of that please post or pm me so I can understand it.
I tried using the time libraries but it seems to have some bugs that aren't expected.
I will bet dollars to doughnuts - hell, I'll bet my life savings against a lollipop - that the bug is not in the libraries, but in your code.
I assume you found the following code where I directed you:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* localtime example */
#include <stdio.h>
#include <time.h>
int main ()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "Current local time and date: %s", asctime (timeinfo) );
return 0;
}
time_t is a integral type which represents seconds since midnight Jan 1 1970.
C structs like struct tm are like classes but they have no member functions or inheritance, only public members.
We call time to set rawtime to the current time.
We call localtime to get a struct tm representing the current time. It contains various members representing parts of the date.