c++ - How do I make boundaries when inputting time? -
i'm typing program right , requires me input departure , arrival time. how can make input value has within 0:00 , 23:59?
right have
cout << "\n\nenter departure time on first day of trip : "; cin >> departure_time; cout << "\n\nenter arrival time on last day of trip : "; cin >> arrival_time; and want make can't time outside of boundaries.
try using while loop test if departure_time , arrival_time within bounds. based on result either move on rest of program or redirect user start.
this can implemented multitude of ways. without knowing how plan use data , solely based on code provided example should illustrate idea of while loops ability check invalid input.
int main() { beginning: double departure_time = 0.00, arrival_time = 0.00; cout<<"\n\nenter departure time on first day of trip : "; cin>> departure_time; cout<<"\n\nenter arrival time on last day of trip : "; cin>> arrival_time; while (departure_time < 0.00 || arrival_time > 23.59) { cout<<"\n\ntry again..."; cin.clear(); cin.sync(); goto beginning; } return 0; } this quick implementation illustrate point. hope helps.
Comments
Post a Comment