Missing 0 When Printing Out?

I have to write a program that takes two times in HH:MM format and tells the difference between them. I need to print out the times (ie: the difference between 10:32 am and 10:40 am is 8 minutes).
I got my program mostly working great! The one problem is that if the number after the colon is a zero, it won't print out. However, it works fine with any other zeros.

input:
10:03 am
output:
10:3 am

input:
10:30 am
output:
10:30 am

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <string> 
using namespace std; 

int computeDifference(int hour1, int hour2, int min1, int min2, 
bool isAm1, bool isAm2);


int main ()
{	int hour1, hour2, min1, min2;
	bool isAm1 = 0, isAm2 = 0;
	char time1, time2, answer, ch;
	
	do 
	{
		cout << "Enter start time in 'HH:MM xm' format, where 'xm' is 'am' or 'pm' for AM or PM =>";
		//Store : and m as character values
		cin >> hour1 >> ch >> min1 >> time1 >> ch;
		
		if(hour1 > 12)
		{
			cout << "Invalid hour number. Please enter an hour between 1 and 12 =>";
			cin >> hour1;
		}
		
		if(min1 > 59)
		{
			cout << "Invalid minute number. Please enter a minute between 0 and 59 =>";
			cin >> min1;
		}
		
		if ((time1 == 'a')|| (time1 == 'A'))
		{
			isAm1 = 1;
		}
		
		cout << "Enter start time in 'HH:MM xm' format, where 'xm' is 'am' or 'pm' for AM or PM =>";
		cin >> hour2 >> ch >> min2 >> time2 >> ch;
		
		if (hour2 > 12)
		{ 
			cout << "Invalid hour number. Please enter an hour between 1 and 12 => ";
			cin >> hour2;
		}
		
		if (min2 > 59)
		{
			cout << "Invalid minute number. Please enter a minute between 0 and 59 =>";
			cin >> min2;
		}
		
		if ((time2 == 'a')||(time2 == 'A'))
			{
				isAm2 = 1;
			}
			
			
	
		cout << "There are " << computeDifference(hour1, hour2, min1, min2, isAm1, isAm2) << " minutes (" <<  (computeDifference(hour1, hour2, min1, min2, isAm1, isAm2))/60  << " hours and " <<  (computeDifference(hour1, hour2, min1, min2, isAm1, isAm2))%60  << " minutes) between " << hour1 << ":" << min1 <<" " << time1 << "m and " << hour2 << ":" << min2 <<" " << time2 <<"m" <<  endl;
		cout << "Enjoy the future!" << endl;
		cout << "Would you like to restart the operation? Y/N ";
		cin >> answer;
	}
	while ((answer == 'y')|| (answer == 'Y'));
	
	
	return 0;
}

int computeDifference(int hour1, int hour2, int min1, int min2, bool isAm1, bool isAm2)
{
	int Difference, minutes1, minutes2;
	
	if(isAm1)
	{
		if ((hour1 >= 1) && (hour1 < 12))
		{hour1 += 12;}
	}
	
	if(isAm2)
	{
		if ((hour2 >= 1) && (hour2 < 12))
		{hour2 += 12;}
	}

	minutes1 = (hour1 * 60) + min1;
	minutes2 = (hour2 * 60) + min2;
	
	if ((hour1 >= hour2) || ((hour1 == hour2) && (min1 > min2)))
	{
		minutes2 += 1440;
	}

	Difference = minutes2 - minutes1;
	
	if (Difference > 1440)
	{
		Difference -= 1440;
	}

	
	return Difference;

}
		
Last edited on
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <iomanip> // for std::setw, std::setfill

int main()
{
    int m = 8 ;
    std::cout << m << '\n' // 8
              << std::setw(2) << std::setfill('0') << m << '\n' ; // 08
              // see: https://stdcxx.apache.org/doc/stdlibug/28-3.html#2832
}
Topic archived. No new replies allowed.