Program closes after input stuck

I want to enter all input and after that, for the program to not end. I am stuck on this because I tried everything. Either my code is affecting this or I am doing something that is not right. Please let me know what the problem is.

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 printTime12(int hour, char ch, int minutes);
void printTime24(int hour, char ch, int minutes);
bool userWantsToContinue();
//========================================================

//========================================================
// main - entry point for all C and C++ programs
int main()
{
do
{
int hour = 0;
char ch;
int minutes = 0;

getTime24(hour, ch, minutes); // function call
printTime12(hour, ch, minutes); // function call
printTime24(hour, ch, minutes); // function call
}
while (userWantsToContinue()); // Y/N user input
}
//========================================================

//============================================
// Function: getTime24
// Description: gets valid time (hour and minute) expressed as per 24 hour clock
// @return void - none, but it will prompt the user for a time in 24 hour format,
//validate the input, and pass back to the caller (by reference) the hour and minute values entered
//============================================
void getTime24(int& hour, char& ch, int& minutes)
{
// prompt user for a time in 24 hour format
cout << "Enter a time in 24 hour format (for example 13:45): ";
cin >> hour >> ch >> minutes;

// validate input and pass back to the caller
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;
}
}

//============================================
// Function: convertTime24to12
// Description: converts 24-hour clock hour to 12-hour clock hour
// @return void - none, but converts between 12 and 24
//============================================
void convertTime24to12(int& hour12, char& a)
{
a = 'p';

if (hour12 == 0)
{
hour12 = hour12 + 12;
a = 'a';
}

if (hour12 >= 1 && hour12 <= 11) // Conversion basically in this instance. Pretty straightforward.
{
a = 'a';
}

if (hour12 >= 13)
{
hour12 = hour12 - 12;
}
}

//============================================
// Function: printTime24
// Description: prints the given time in 24 hour format
// @return void - Time(hour, minute) will be printed in 24 hour format
//============================================
void printTime24(int hour, char ch, int minutes)
{
// printing the time in 24 hour format
cout << "That time in 24 hour format is: " << hour << ch << minutes << endl;
}

//============================================
// Function: printTime12
// Description: prints the given time in 12 hour format
// @return void - Time(hour, minute) will be printed in 12 hour format
//============================================
void printTime12(int hour, char ch, int minutes)
{
// converts 24 to 12 hour format and prints the time
convertTime24to12(hour, ch);
cout << "That time in 12 hour format is: " << hour << ":" << minutes << " " << ch << "m" << endl;
}

//============================================
// Function: userWantsToContinue
// Description: prompts the user to see if they want to continue
// @return void - returns true if user says yes, false otherwise
//============================================
bool userWantsToContinue()
{
// Asking if the user wants to continue
// If user enters Y (yes), then the program will start over again
// If user enters N (no), then the program will end
cout << "\nWould you like to continue (Y/N)? ";
char c;
cin >> c;
return c == 'Y' || c == 'y';
}
I want to enter all input and after that, for the program to not end.


Seems to work fine. The user chooses "y" to not end, it doesn't end.

http://cpp.sh/3ku7y
Hello Maxster,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


Like Repeater the program ran fine for me. If you have a problem it could be your operating system or IDE/compiler.

The question is; what is your operating system and IDE/compiler?

I did notice a problem with the output when a time entered of say 16:02 the output is:

Enter a time in 24 hour format (for example 13:45): 16:02
That time in 12 hour format is: 4:2 pm
That time in 24 hour format is: 16:2
Would you like to continue (Y/N)?


Not quite what you want.

I only changed the function "printTime24" so far.

Give this a try:
1
2
3
4
5
6
void printTime24(int hour, char ch, int minutes)
{
	// printing the time in 24 hour format
	cout << "That time in 24 hour format is: " << hour << ch;
	minutes < 10 ? cout << "0" << minutes : cout << minutes << endl;
}

When you enter a value for "hour" and "minutes" it does not store a leading zero. So, in your output you need to put in a zero if needed.

You will also have to change the "printTime12" function.

Hope that helps,

Andy
I still don't know how to fix void printTime24. I include a zero, but it looks like this: "12:150". I don't want that. How can I fix this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
#include<iomanip>
using namespace std;
void printTime(int hour, int minute, int second);
void main()


{
	 printTime(5,2,6);



	system("pause");
}
void printTime(int hour, int minute, int second)
{
	cout << setfill('0') << setw(2) << hour << ":" << setw(2) << minute << ":" << setw(2) << second;


}
Last edited on
Final code:

#include <iostream>
#include <string>

using namespace std;

//=============================================
//function prototyping
//============================================
// Function: getTime24
// Description: gets valid time (hour and minute) expressed as per 24 hour clock
// @return void - none, but it will prompt the user for a time in 24 hour format,
//validate the input, and pass back to the caller (by reference) the hour and minute values entered
//============================================
void getTime24(int& hour, char& ch, int& minutes);
//============================================
// Function: convertTime24to12
// Description: converts 24-hour clock hour to 12-hour clock hour
// @return void - none, but converts between 12 and 24
//============================================
void convertTime24to12(int& hour12, char& a);
//============================================
// Function: printTime24
// Description: prints the given time in 24 hour format
// @return void - Time(hour, minute) will be printed in 24 hour format
//============================================
void printTime24(int hour, char ch, int minutes);
//============================================
// Function: printTime12
// Description: prints the given time in 12 hour format
// @return void - Time(hour, minute) will be printed in 12 hour format
//============================================
void printTime12(int hour, char ch, int minutes);
//============================================
// Function: userWantsToContinue
// Description: prompts the user to see if they want to continue
// @return void - returns true if user says yes, false otherwise
//============================================
bool userWantsToContinue();

//========================================================
// main - entry point for C++
int main()
{
do
{
int hour = 0;
char ch;
int minutes = 0;

getTime24(hour, ch, minutes); // function call
printTime12(hour, ch, minutes); // function call
printTime24(hour, ch, minutes); // function call
}
while (userWantsToContinue()); // Y/N user input

cin.get();
cin.ignore();
}
//========================================================

void getTime24(int& hour, char& ch, int& minutes)
{
// prompt user for a time in 24 hour format
cout << "Enter a time in 24 hour format (for example 13:45): ";
cin >> hour >> ch >> minutes;

// validate input and pass back to the caller
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) // Conversion basically in this instance. Pretty straightforward.
{
a = 'a';
}

if (hour12 >= 13)
{
hour12 = hour12 - 12;
}
}

void printTime24(int hour, char ch, int minutes)
{
// printing the time in 24 hour format
cout << "That time in 24 hour format is: " << hour << ch;
minutes < 10 ? cout << "0" << minutes : cout << minutes << endl;
}

void printTime12(int hour, char ch, int minutes)
{
// converts 24 to 12 hour format and prints the time
convertTime24to12(hour, ch);
if(minutes < 10)
cout << "That time in 12 hour format is: " << hour << ":0" << minutes << " " << ch << "m" << endl;
else
cout << "That time in 12 hour format is: " << hour << ":" << minutes << " " << ch << "m" << endl;
}

bool userWantsToContinue()
{
// Asking if the user wants to continue
// If user enters Y (yes), then the program will start over again
// If user enters N (no), then the program will end
cout << "\n\nWould you like to continue (Y/N)? ";
char c;
cin >> c;
return c == 'Y' || c == 'y';
}
Topic archived. No new replies allowed.