only able to enter input after first prompt, then program finishes c++ -
so i'm trying put program project, , it's behaving strangely me. here's example of happens in console:
compute completion time current time , waiting period
current time:
enter 24 hour time in format hh:mm 12:50
waiting time:
enter 24 hour time in format hh:mm completion time in 24 hour format:
4644952:4198980
enter y or y continue, other halts
process returned 0 (0x0) execution time : 3.927 s
press key continue.
the part in bold part input , runs through cout statements without letting me input second pieces of data. outputs garbage number calculation function. here 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 current time , waiting period \n"; { runloop(); cout << "enter y or y continue, 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 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 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 pretty new apologize if there obvious missing. i'm hoping 1 of can me out here, i'm totally stumped why not working! thanks!
the problem having cin
entering error state , every subsequent call cin
automatically fail , program continue on. when time in:
void getcurrenttime(int& c_hours, int& c_minutes) { cout << "current time:\n" << "enter 24 hour time in format hh:mm "; cin >> c_hours >> c_minutes; }
you not eating :
exist in 12:50
. such tries insert :
minutes , fails.
what need call cin.get()
eat :
, minutes.
void getcurrenttime(int& c_hours, int& c_minutes) { cout << "current time:\n" << "enter 24 hour time in format hh:mm "; cin >> c_hours; cin.get(); // eats : cin >> c_minutes; }
Comments
Post a Comment