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
|
#include <iostream>
#include <string>
using namespace std;
//function prototyping
void getTime24(int& hour, char& ch, int& minutes);
void convertTime24to12(int& hour12, char& a);
void printTime24(int hour, char ch,int minutes);
void printTime12(int hour,char ch,int minutes);
char userWantsToContinue(char ans);
void main()
{
int hour=0;
char ch;
int minutes=0;
getTime24(hour, ch, minutes);
printTime24(hour,ch,minutes);
printTime12(hour, ch, minutes);
}
void getTime24(int& hour, char& ch, int& minutes)
{
cout << "Enter a time in 24 hour format (for example 13:45): ";
cin >> hour >> ch >> minutes;
while (hour < 0 || hour >= 24 || minutes < 0 || minutes >= 60)
{
cout << "I'm sorry the information you entered is not valid. Please try again " << endl;
cin >> hour >> ch >> minutes;
}
}
void convertTime24to12(int& hour12, char& a)
{
if (hour12 >= 13)
{
hour12 = hour12 - 12;
a = 'p';
}
if (hour12==12)
{
a='p';
}
if (hour12==0)
{
hour12=hour12+12;
a= 'a';
}
if (hour12>=1 && hour12 <=11)
a='a';
}
void printTime24(int hour,char ch,int minutes)
{
cout << "That time in 24 hour format is: " << hour << ch << minutes<<endl;
}
void printTime12(int hour,char ch, int minutes)
{
convertTime24to12(hour, ch);
cout << "That time in 12 hour format is: " << hour <<":"<<minutes<<" "<<ch<<"m"<<endl;
}
|