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
|
//Program converts from 24hr time to 12hr time.
#include <iostream>
using namespace std;
void introduction();
//Postcondition: Description of program is written on the screen.
void input_time(int& hours, int& minutes, char& A, char& P);
//Precondition: User enters time correctly.
//Postcondition: The value of hours has been set to the
//the variable 24hr_time.
int time_conversion(int& hours, int& minutes, char& A, char& P);
//Precondition: Obtains hours, minutes, A and P from input_time function.
//Postcondition: converts time from 24hr time to 12hr time and subtracts 12
//hours if hour is greater then 12.
void output_time(int& hours, int& minutes, char& A, char& P);
//Postcondition: Outputs the converted time.
int main()
{
int _24hr_time, minutes;
char ans, AM, PM;
introduction();
do
{
input_time(_24hr_time, minutes, AM, PM);
time_conversion(_24hr_time, minutes, AM, PM);
output_time(_24hr_time, minutes, AM, PM);
cout << "Peform another conversion?"
<< " (Type y of yes or n for no): ";
cin >> ans;
} while (ans == 'y' || ans == 'Y');
return 0;
}
//Uses iostream:
void introduction()
{
cout << "This program converts 24 hour time to 12 hour time.\n";
}
//Uses iostream:
void input_time(int& hours, int& minutes, char& A, char& P)
{
cout << "Enter the hour portion of the 24 hour time you wish to convert to 12 hour time: ";
cin >> hours;
cout << "Enter the minutes portion of the 24 hour time you wish to convert to 12 hour time: ";
cin >> minutes;
}
//Uses iostream:
int time_conversion(int& hours, int& minutes, char& A, char& P)
{
{
if (hours > 12)
hours = hours - 12;
return 0;
}
}
//Uses iostream:
void output_time(int& hours, int& minutes, char& A, char& P)
{
cout << "The converted time is: " << hours << ":" << minutes << endl;
}
|