Need help with Y/N loop with functions

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;
}
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
#include <iostream>

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);
bool userWantsToContinue() ;

int main() // *** int main() : see http://www.stroustrup.com/bs_faq2.html#void-main
{
    do
    {
        int hour = 0;
        char ch;
        int minutes = 0;

        getTime24(hour, ch, minutes);
        printTime24(hour, ch, minutes);
        printTime12(hour, ch, minutes);
    }
    while( userWantsToContinue() ) ;
}

bool userWantsToContinue()
{
    std::cout << "continue (Y/N)? " ;
    char c ;
    std::cin >> c ;
    return c == 'Y' || c == 'y' ;
}
Thank you so much! It works!
Topic archived. No new replies allowed.