Reputation: 45
So I'm trying to put together this program for a project, and it's behaving strangely for me. Here's an example of what happens in the console:
Compute completion time from current time and waiting period
Current time:
Enter 24 hour time in the format HH:MM 12:50
Waiting time:
Enter 24 hour time in the format HH:MM Completion time in 24 hour format:
4644952:4198980
Enter Y or y to continue, any other halts
Process returned 0 (0x0) execution time : 3.927 s
Press any key to continue.
The part in bold is the only part I could input and then it just runs through the cout statements without letting me input the second pieces of data. Then outputs some garbage number from the calculation function. Here is my code:
#include <iostream>
using namespace std;
void getEndTime(int c_hours, int c_minutes, int w_hours, int w_minutes, int& e_hours, int& e_minutes);
void getCurrentTime(int& c_hours, int& c_minutes);
void getWaitingTime(int& w_hours, int& w_minutes);
void runLoop();
int main()
{
char select;
cout << "Compute completion time from current time and waiting period \n";
do {
runLoop();
cout << "Enter Y or y to continue, any other halts";
cin >> select;
} while (select == 'y' || select == 'Y');
return 0;
}
void runLoop()
{
int current_hours, current_minutes, waiting_hours, waiting_minutes;
int end_hours, end_minutes;
getCurrentTime(current_hours, current_minutes);
getWaitingTime(waiting_hours, waiting_minutes);
getEndTime(current_hours, current_minutes, waiting_hours, waiting_minutes, end_hours, end_minutes);
cout << "Completion time in 24 hour format:\n" << end_hours << ":" << end_minutes << endl;
}
void getCurrentTime(int& c_hours, int& c_minutes)
{
cout << "Current time:\n"
<< "Enter 24 hour time in the format HH:MM ";
cin >> c_hours >> c_minutes;
}
void getWaitingTime(int& w_hours, int& w_minutes)
{
cout << "Waiting time:\n"
<< "Enter 24 hour time in the format HH:MM ";
cin >> w_hours >> w_minutes;
}
void getEndTime(int c_hours, int c_minutes, int w_hours, int w_minutes, int& e_hours, int& e_minutes)
{
if ((c_hours + w_hours) >= 24) {
e_hours = (c_hours + w_hours - 24);
}
else {
e_hours = (c_hours + w_hours);
}
if ((c_minutes + w_minutes) >= 60) {
e_hours += 1;
e_minutes = (c_minutes + w_minutes) - 60;
}
else {
e_minutes = c_minutes + w_minutes;
}
return;
}
I am pretty new to this so I apologize if there is something obvious I am missing. But I'm hoping one of you can help me out here, I'm totally stumped as to why this is not working! Thanks!
Upvotes: 0
Views: 914
Reputation: 180415
The problem you are having is cin
is entering an error state and every subsequent call to cin
will automatically fail and the program will continue on. When you get the time in:
void getCurrentTime(int& c_hours, int& c_minutes)
{
cout << "Current time:\n"
<< "Enter 24 hour time in the format HH:MM ";
cin >> c_hours >> c_minutes;
}
You are not eating the :
that exist in 12:50
. As such it tries to insert the :
into minutes and fails.
What you need to do is call cin.get()
to eat the :
and then get the minutes.
void getCurrentTime(int& c_hours, int& c_minutes)
{
cout << "Current time:\n"
<< "Enter 24 hour time in the format HH:MM ";
cin >> c_hours;
cin.get(); // eats :
cin >> c_minutes;
}
Upvotes: 2