24 hour notation to 12 hour notation problem.

Write your question here.
#include "stdafx.h"
#include<iostream>



using namespace std;
void time(int& hours, char& ampm, int& minutes);
void change(int& hours, char& ampm, int& minutes);
void outtime(int hours, char ampm, int minutes);

int main()

{
int hours, minutes;
char ampm;
char again = 'y';
while(again=='y') {
time(hours, ampm, minutes);
change(hours, ampm, minutes);
outtime(hours, ampm, minutes);
cout << "Enter Y or y to continue, anything else quits." << endl;
cin >> again;
}

return 0;
}
void time(int& hours,char& ampm, int& minutes) {
char colon;
cout << "Enter 24-hour time in the format HH:MM" << endl;
cin >> hours >> colon >> minutes;
}
void change(int& hours, char& ampm, int& minutes) {
if (hours > 12) {
hours = hours - 12;
ampm = 'p';
}
else if (hours == 12) ampm = 'p';
else ampm = 'a';


}
void outtime(int hours, char ampm, int minutes) {
if (ampm == 'p') {
cout << "Time in 12- hour format:" << endl;
cout << hours << ":" << minutes << " PM" << endl;
}
else(ampm == 'a');
cout << "Time in 12-hour format:" << endl;
cout << hours << ":" << minutes << " AM" << endl;

}


Everytime I run this program i end up getting the time conversions for both am and pm as if my last if else statement isnt working. Ive been looking it over for a while now and still cant spot the problem.
@MAZambelli4353,

Please use code tags when posting code. It allows people to run your code in C++ shell without having to download it and reformat it.

If you indent carefully you will spot errors like this. Watch out for extra semicolons closing if blocks early, and indent your if blocks so that their structure is easily visible.

In this case the last 'else if' could just be an 'else' should you want that.

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
#include<iostream>
using namespace std;
void time( int& hours, char& ampm, int& minutes );
void change( int& hours, char& ampm, int& minutes );
void outtime( int hours, char ampm, int minutes );

int main()
{
   int hours, minutes;
   char ampm;
   char again = 'y';
   while ( again=='y' )
   {
      time( hours, ampm, minutes );
      change( hours, ampm, minutes );
      outtime( hours, ampm, minutes );
      cout << "Enter Y or y to continue, anything else quits." << endl;
      cin >> again;
   }
}

void time( int& hours, char& ampm, int& minutes )         // <==== what is the purpose of ampm here?
{
   char colon;
   cout << "Enter 24-hour time in the format HH:MM" << endl;
   cin >> hours >> colon >> minutes;
}

void change( int& hours, char& ampm, int& minutes )         // <==== what is the purpose of minutes here?
{
   if ( hours > 12 )
   {
      hours = hours - 12;
      ampm = 'p';
   }
   else if ( hours == 12 ) ampm = 'p';
   else ampm = 'a';
}

void outtime( int hours, char ampm, int minutes )
{
   if ( ampm == 'p' )
   {
      cout << "Time in 12- hour format:" << endl;
      cout << hours << ":" << minutes << " PM" << endl;
   }
   else if ( ampm == 'a' )                                 // REMOVE SEMICOLON; could just use else
   {                                                       // <=== need opening brace
      cout << "Time in 12-hour format:" << endl;
      cout << hours << ":" << minutes << " AM" << endl;
   }                                                       // <=== need closing brace
}
Last edited on
Thankyou!!!
Topic archived. No new replies allowed.