Hi, I am trying to write this program and obviously I have problems with it. I am not sure where and what I am doing wrong, anyone's help is much appreciated, thanks for the time.
Code:
#include <iostream.h>
#include <iomanip.h>
#include <assert.h>
class Time
{
public:
Time(int h=0, int m=0, int s=0);
void printStandard();
void add(Time t);
void subtract(Time t);
void getFromKbd();
private:
int hour;
int min;
int sec;
};
Time::Time(int h, int m, int s)
{
hour = (h>=0 && h<24) ? h:0;
min = (m>=0 && m<60) ? m:0;
sec = (s>=0 && m<60) ? s:0;
}
void Time::printStandard()
{
cout << ((hour % 12 == 0) ? 12 : hour % 12) << ':'
<< (min < 10 ? "0" : "") << min << ':'
<< (sec < 10 ? "0" : "") << sec
<< (hour < 12 ? " AM" : " PM");
}
void Time::add() // Adds t to the object through which the function is called
{
Time t;
cout << "Sum of the time is " << t+t.getFromKbd();
}
void Time::subtract() // Subtracts t from the object through which the function is called
{
cout << "Difference of the time is " << t-t.getFromKbd();
}
void Time::getFromKbd() // t.getFromKbd() will get values for t.hour, t.min, and t.sec from the keyboard, allows user to input as military time
{
cout << "Enter the hour(s): ";
cin >> t.hour;
cout << "Enter the minute(s): ";
cin >> t.min;
cout << "Enter the second(s): ";
cin >> t.sec;
}
int main()
{
Time t(12, 45, 30);
t.printStandard(); // should display 12:45:30 AM or PM
t.getFromKbd(); // user inputs his/her time
t.add(); // adds user time to 12:45:30
t.printStandard(); // displays sum of times
t.subtract(); // subtracts both times
t.printStandard(); // prints times
return 0;
}