I have a C++ programming assignment due today and I am having trouble getting it to work correctly. Here is the assignment:
Write a program with a class called CheapWatch that provides the time of day in a program. Three unsigned integers should be used to represent time:
The first integer should represent the hour.
The second integer should represent the minutes.
The third integer should represent the seconds.
Include in the program a type conversion constructor that converts a long integer representing the number of seconds from midnight into your three-integer (hour, minute, second) representation. For example, if the long integer 18637 should convert to 5:10:37, you may use the following formula:
Elapsed seconds = (hours * 3600) + (minutes * 60) + seconds.
Use military time. For example, 3:40 PM is represented as 15:40:00.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
#include <iostream>
#include <iomanip>
using namespace std;
class cheap_watch
{
private :
unsigned hr , minute , second;
public:
char ch;
cheap_watch(int a,int b,int c,char cha)
{
hr=a;
minute=b;
second=c;
ch=cha;
}
cheap_watch(unsigned num)
{
hr=static_cast<int>(num/3600);
minute= static_cast<int>(((num/3600)-hr )* 60);
second=static_cast<int>((((( (num/3600)-hr )*60)-minute))*60);
};
void print_in_military_form()
{
if(ch == 'p' || ch =='P')
hr=hr+12;
if (hr<10)
cout<<"0"<<hr<<":";
else
cout<<hr<<":";
if(minute<10)
cout<<"0"<<minute<<":";
else
cout<<minute<<":";
if(second<10)
cout<<"0"<<second<<endl;
else
cout<<second<<endl;
}
};
void main()
{
unsigned seconds;
cout<<"please enter the number of seconds after midnight"<<endl;
cin>>seconds;
cheap_watch time(seconds);
time.print_in_military_form();
}
|
This is what I have so far, when I run it I don't get the correct output.