Military & Standard Time

closed account (ybo2y60M)
I've search the web, and solved all the errors that appeared till I got a clean build.

Now any time I run the code I run into this issue.

enter hour of mtime : 19

Mmin: 26

Msec: 05

standard time is 12:00:00 AM

military time is 00:00:00

I can't figure out why this isn't displaying any of my inputs.




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef TIME_H
#define TIME_H

class Time
{
public:
	Time(int = 0, int = 0, int = 0);
	~Time();
	int hour; // valid values are 0 to 23
	int minute; // valid values are 0 to 59
	int second; // valid values are 0 to 59
	void setTime(int, int, int); // function that checks if inputs are valid
	void printUniversal(); // prints in HH:MM:SS format
	void printStandard(); // prints in HH:MM:SS AM/PM format
	static int count; //counter
};

#endif 


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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//(HEADER FILE called Time.h)





//(File that contains definitions - Time.cpp)
#include "stdafx.h"
#include "Time.h" //header file that contains the Time class file
#include <iostream>
#include <iomanip>
#include <ctime>

using namespace std;

int Time::count = 10;

Time::Time(int hr, int min, int sec)
{
	hour = hr; minute = min; second = sec;
	count++;
}


Time::~Time()
{
	count--;
}

void Time::setTime(int hr, int min, int sec)
{
	hour = (hr >= 00 && hr < 24) ? hr : 00; // checks if hour input is valid
	minute = (min >= 00 && min < 60) ? min : 00; // checks if minute input is valid
	second = (sec >= 00 && sec < 60) ? sec : 00; // checks if seconds input is valid
}



void Time :: printUniversal()
{
	cout << setfill('0') << setw(2) << hour << ":" << setw(2) << minute << ":" << setw(2) << second;
}

void Time::printStandard()
{
	cout << ((hour == 00 || hour == 12) ? 12 : hour % 12) << ":" << setfill('0') << setw(2) << minute << ":" << setw(2) << second << (hour < 12 ? " AM" : " PM");
}


//(Where I implement the functions - main.cpp)
//#include "Time.h"
//#include <iostream>
//using namespace std;



int main()
{
	int hour, minute, second;

	Time t; //t is the time object //(PROBLEM!!!-Dont understand how to write the correct parameters)
	 //test is also a time object //(PROBLEM!!!-Dont understand how to write the correct parameters)
	//Time *tp = new Time;
	//Time *tarray = new Time[5];



	cout << "Enter hour in military time ";
	cin >> hour;
	cout << "Enter minute ";
	cin >> minute;
	cout << "Enter second ";
	cin >> second;



	cout << "\nThe standard time is ";
	t.printStandard(); //(PROBLEM!!!- I have the number 12 appearing right after AM and i can't get rid of it. )
	cout << "\nThe universal time is ";
	t.printUniversal();
	cout << endl;
	return 0;
} // end main 
Why do you include ctime if you are not using it?

Also move line 61 to line 74 (after getting hour, minute and second). Then call it with the constructor so Time t(hour, minute, second);
closed account (ybo2y60M)
Thank you giblit.

I can't believe I didn't notice this before, guess it always help to have a 2nd pair of eyes look it over.
Topic archived. No new replies allowed.