I need to write a program with a class called CheapWatch

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.
add a prtottype of print_int_miltary_form() in the cheap_watch class declaration, and in the type casting ctor, add hr>12?ch='p':ch=a and it should work. I'm gonna try it out on my comp, and i'lll tell you the results.
EDIT: sorry, I just brealised that print_in_military_form() is already inside the class, sorry. And, you actualy don't need to add that ch assignment. But you do need to add some type-casting to double, or you'l get all zeros except for hours.
Last edited on
Thanks, but how do I do that?
Try this framework.

1: Why 'p' or 'P' your not asking for it anywhere
2: A calculation 71100 % 3600 = 19, so there is no need to convert as you did in 31 - 35

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
#include <iostream>
using namespace std;

class cheap_watch {
    int Hr, Min, Sec;
  public:
    void set_values ( int );
    void Show ( void );
};

void cheap_watch::set_values ( int ) {
	// Code that will convert seconds to Hr/Min/Sec
	}
	
void cheap_watch::Show ( void ) {
	// Output formatted military time
	}

int main () {
	int Seconds;
  	cheap_watch Clock;
  	
  	cout << "Enter seconds: " << endl;
  	cin >> Seconds;
  	Clock.set_values ( Seconds );
  	Clock.Show ();
  	return 0;
	}
Thanks, but this just displays the seconds I entered. How do I get it to print the hour, minutes and seconds in military time? How would I finish that?
You sort of worked it out in 23 - 25. I never checked to see if it worked but it could be done like this

1
2
3
4
5
void cheap_watch:: set_values ( int Secs ) {
    Hr = Secs / 3600;
    Min = (Secs % 3600) / 60;
    Sec = Secs % 60;
    }


Now all you have to do is designed the code in Show that will display HH:MM:SS and set_values has already determined 24 hr.
I'm confused about this part "Now all you have to do is designed the code in Show that will display HH:MM:SS and set_values has already determined 24 hr"

Do I add this in after the return 0; and what is "designed the code"?

Sorry this is really my first programming class
Your code to output formatted string goes in line 16. Please qualify what you mean by first programming class. Have you not learned anything about C++ before being given this assignment?
No, I mean this is the first C++ class I've taken and any help would be appreciated. I don't have help from anyone else.
Topic archived. No new replies allowed.