Nov 8, 2018 at 2:04am UTC
I've got this program to prompt for current time in 24 hour format. I use functions to convert the time and print in 12 hour format. I got all of this code working. The only problem is for the user to enter, if they would like to continue with Yes or end it with a No. I need to use it in a function called userWantsToContinue.
Code:
#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);
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)
{
a = 'p';
if (hour12 == 0)
{
hour12 = hour12 + 12;
a = 'a';
}
if (hour12 >= 1 && hour12 <= 11)
{
a = 'a';
}
if (hour12 >= 13)
{
hour12 = hour12 - 12;
}
}
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;
}
Nov 8, 2018 at 3:37pm UTC
Thank you so much! It works!