First do the math on paper & pencil. Once you get that right then coding it will be simple.
If the hours is greater than 12 just subtract 12 and the A.M./P.M. flag will be 'P'. Otherwise if the hours is less than 12 the flag will be 'A' and you don't have to do anything to the hours.
Ancient Dragon
Achieved Level 70
32,140 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,577
Skill Endorsements: 69
put it in the convert function that you started (see the last line that you posted) int convertTo12Hour(int hours, char& type);{ Delete that semicolon!
Ancient Dragon
Achieved Level 70
32,140 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,577
Skill Endorsements: 69
First do the math on paper & pencil. Once you get that right then coding it will be simple.
If the hours is greater than 12 just subtract 12 and the A.M./P.M. flag will be 'P'. Otherwise if the hours is less than 12 the flag will be 'A' and you don't have to do anything to the hours.
Maybe you can avoid that, I am thinking something like this.
void Convert( int& hours )
{
hours = hours % 12;
}
BevoX
Junior Poster in Training
57 posts since Jan 2009
Reputation Points: 77
Solved Threads: 13
Skill Endorsements: 0
Maybe you can avoid that, I am thinking something like this.
void Convert( int& hours )
{
hours = hours % 12;
}
The problem is that will not tell whether it's am or pm.
Ancient Dragon
Achieved Level 70
32,140 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,577
Skill Endorsements: 69
using an if else statement but what function i call i can put it in words but that's where i have a hard problem putting it into code
i know it would be something like
if hours < 12
pm = 12- hours ?
cout>>pm?
if( hours < 12)
{
pm = 'A';
}
else
{
hours -= 12;
pm = 'P';
}
Ancient Dragon
Achieved Level 70
32,140 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,577
Skill Endorsements: 69
yes almost but i think the if else syntax is wrong?
you mean what I posted before? why do you think it's wrong?
Ancient Dragon
Achieved Level 70
32,140 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,577
Skill Endorsements: 69
because i am getting complier errors when added onto my new code
#include <iostream>
using namespace std;
void input(int& hours, int& minutes);
void output(int hours, int minutes, char type);
int convertTo12Hour(int hours, char& type);
int main() {
int hours;
int minutes;
char type;
char answer;
do
{
input(hours, minutes);
hours = convertTo12Hour(hours, type);
output(hours, minutes, type);
cout << "Perform another calculation? (y/n): ";
cin >> answer;
} while ((answer == 'Y') || (answer == 'y'));
return 0;
}
void input(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;
}
//
// Displays a time in 12 hour notation
//
void output(int hours, int minutes, char type) {
cout << "The time converted to 12 hour format is: " << hours << ":";
//
// special handling for leading 0s on the minutes
//
cout.width(2);
cout.fill('0');
cout << minutes;
//
if (type == 'A')
cout << " A.M." << endl;
else
cout << " P.M." << endl;
}
void Convert( int& hours )
{
hours = hours % 12;
}
if( hours < 12)
{
pm = 'A';
}
else
{
hours -= 12;
pm = 'P';
}
errors
Error 1 error C2059: syntax error : 'if'
Error 2 error C2143: syntax error : missing ';' before '{' 65 lab4
Error 3 error C2447: '{' : missing function header (old-style formal list?) 65 lab4
Error 4 error C2059: syntax error : 'else' c
Error 5 error C2143: syntax error : missing ';' before '{' lab4
Error 6 error C2447: '{' : missing function header (old-style formal list?)
they all centralize around the convert and if else statement
You have all those errors because you failed to put that if statement inside any function. Check your { and } and you will see your mistake.
Ancient Dragon
Achieved Level 70
32,140 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,577
Skill Endorsements: 69