Error occur....

Write a program to convert the time from 24-hour notation to 12-hour notation and vice versa. Your program must be menu driven, giving the user the choice of converting the time between the two notations. Furthermore, your program must contain at least the following function: a function to convert the time from 24-hour notation to 12-hour notation, a function to convert the time from 12-hour notation to 24-hour notation, a function to display the choices, function(s) to get the input, and function(s) to display the results. (For 12-hour time notation, your program must display AM or PM.)

Answer:

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

void input24(int hours, int minutes);
void output12(int hours, int minutes, char type);
void convert24To12Hour(int hours, char type);


int main()
{
int hours;
int minutes;
char type;
char pm;
char A, P;

cout << "Enter the number hours";
cin >> hours;
cout << "Enter the number minute";
cin >> minutes;

input(hours, minutes);
hours = convert24To12Hour(hours, type);
output(hours, minutes, type);


system("pause")
return 0;
}

void input24(int hours, int minutes)
{
cout << "Enter the hours for the 24 hour time: ";
cin >> hours;
cout << "Enter the minutes for the 24 hour time: ";
cin >> minutes;
}

void output12(int hours, int minutes, char type)
{
cout << "The time converted to 12 hour format is: " << hours << ":";
cout << minutes;

if (type == 'A')
cout << " A.M." << endl;
else
cout << " P.M." << endl;
}

void convert24To12Hour(int hours, char type)
{
hours = hours % 12;
}

if( hours < 12)
{
pm = 'A';
}

else
{
hours -= 12;
pm = 'P';
}

It is showing error because may be I was not able to put that if statement inside any function. Please help me to find out the error sand complete the program with corrected code.
change convert24To12Hour to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void char convert24To12Hour(int hours, char type)
{
hours = hours % 12;
}

char pm;
if( hours < 12)
{
pm = 'A';
}

else
{
hours -= 12;
pm = 'P';
}
return pm;
}
Don't forget to change the prototype as well

Please use code tags: [code]Your code[/code]
See: http://www.cplusplus.com/articles/z13hAqkS/
Actually, line 3 guarantees that hours will always be < 12. So get rid of line 3 as well. Line 14 does the same thing in a different way.

Lastly, if you pass hours into the function as an int, the change to hours will only be local to the function. If you want to see the change in hours in the larger program, you need to pass the variable as a reference. Try:

void convert24To12Hour(int& hours, char& pm);

as your protoype.

By the way, I disagree with @coder777 slightly in style. I would have left the function as a void function and passed both hours and pm as reference arguments. I'm not saying the @coder777 is wrong, just providing a different way of doing it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void convert24To12Hour(int& hours, char& pm);

int main()
{
   int hours;
   char pm;
   ...
   convert24To12Hour(hours, pm);
   ...
}

void convert24To12Hour(int& hours, char& pm)
{
...
}
Last edited on
Topic archived. No new replies allowed.