time error

hi, why is it that when i input " 00 for hours and 01 for minutes " the output is 12: 1 AM ? my brain is already bleeding. thanks in advance.
Last edited on
Because the first hour of the day is 1 AM. So when you put in the zeroth hour of the day, it would technically be the 24th hour of the previous day...but then it rolls back over (subtracts the 24 hours). Think about a 24 hour time systems (Americans use a 12 hour clock) like the military and several nations in the world.

00:30 is 12:30 in the morning
12:30 is 12:30 in the evening (or afternoon)

24:00 is 00:00 is 12:00 midnight
@Volatile Pulse this is the one that i programmed, i can't really express my problems but kindly try this to see what the problem is. :) thanks in advance.

void time ()
{
int hours;
int minutes;
string ampm, choice;

while (1)
{
cout <<"Enter the hour/hours."<<endl;
cin >> hours;
cout<<"Enter the minute/minutes."<<endl;
cin>>minutes;

if(hours > 12)
{
hours -= 12;
ampm = "PM";
}
else
{
ampm = "AM";
}

if ((hours >= 13 || hours >= 24))
{
cout<<"Invalid."<<endl;
}
if (minutes >= 61)
{
cout<<"Invalid."<<endl;
}
else if (hours == 0)
{
cout<<"12 : "<<minutes<<"AM";
}
else
{
cout << hours << ':' << minutes << ampm << endl;

}

{
break;
}
}
Oh, you're asking why it displays "12:1" instead of "12:01"?

To fix this, simply add the iomanip header file into your program and change these lines:
1
2
3
4
5
6
7
8
else if (hours == 0)
{
   cout<<"12 : "<<minutes<<"AM";
}
else
{
   cout << hours << ':' << minutes << ampm << endl;
}

To this:
1
2
3
if (hours == 0)
  hours = 12;
cout << hours << ':' << setw(2) << setfill('0') << minutes << ampm << endl;
@Volatile Pulse
wow! thanks. that really helped. :) but another one, our time should be in 24 hour format, that's why it should have the output " 00: 30 AM " instead of " 12:30 AM " :). again thanks for the previews answer. :)
If it's supposed to print out in 24 hour format, your code has a lot of "extras" that you don't need.

You can remove the string ampm and every assignment of it, you can remove the first if/else conditions, you second if should be less than 0, greater than 23, your third if should be greater than 59 less than 0, your fourth one can be removed altogether, and you should have the output only displayed if the user didn't enter "Invalid" times. Also, you need to move setw(2) and setfill('0'); to the beginning of the cout line.
oh, thanks,. ill try it now. :)
i tried your instruction but still the output is " 00: 1AM "


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

int main()

{
    int hours;
	int minutes;


	while (1)
	{
		cout <<"Enter the hour/hours."<<endl;
		cin >> hours;
		cout<<"Enter the minute/minutes."<<endl;
		cin>>minutes;



		if ((hours < 0 || hours > 23))
		{
		    cout<<"Invalid."<<endl;
		}
		if ((minutes > 59 || minutes < 0))
		{
		    cout<<"Invalid."<<endl;
		}
       else if (hours == 0)
        {
            cout<<"00 : " <<minutes << "AM";
        }
        else
        {
            cout<<"Invalid";
        }


		{
			break;
		}
	}
}
You're getting much closer. Let me show you quickly.
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
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
   int hours = 1;
   int minutes = 1;

   // While the user didn't enter "midnight"
   while (hours != 0 && minutes != 0) {
      // Ask for the time
      cout << "Enter the hour/hours." << endl;
      cin >> hours;
      cout << "Enter the minute/minutes." << endl;
      cin >> minutes;

      // If hours or minutes are out of range
      if ((hours < 0 || hours > 23) || (minutes < 0 || minutes > 59))
         // Display "Invalid."
         cout << "Invalid." << endl;
      // Otherwise
      else
         // Display the time
         cout << setw(2) << setfill('0') << hours << ":" << minutes;
      // Add some blank lines
      cout << "\n\n";
   }

   return 0;
}
oh, i forgot the iomanip thing. :) but thanks again and again. :)
Topic archived. No new replies allowed.