То, что я до сих пор:Как я могу считывать входные данные и форматировать вывод?
cout << "Please enter your destination travel start time (HH:MM): " << endl;
cin >>dest_startHOUR>>dest_startMIN;
cout << "Please enter your destination travel end time (HH:MM): " << endl;
cin >>dest_endHOUR>>dest_endMIN;
cout << "Please enter your home travel start time (HH:MM): " << endl;
cin >>home_startHOUR>>home_startMIN;
cout << "Please enter your home travel end time (HH:MM): " << endl << endl;
cin >>home_endHOUR>>home_endMIN;
cout << "Departed from " << home_city << " at " << dest_startHOUR << ":" << dest_startMIN << "." << endl;
cout << "Traveled " << dest_miles << " miles to " << dest_city << ", arrived at " << dest_endHOUR << ":" << dest_endMIN << ". " << "Travel time, " << dest_endHOUR - home_startHOUR << "." << (dest_endMIN - home_startMIN)/60 << " hours."<< endl << endl;
И это дает мне следующий вывод:
Please enter your destination travel start time (HH:MM):
04:30
Please enter your destination travel end time (HH:MM):
Please enter your home travel start time (HH:MM):
Please enter your home travel end time (HH:MM):
Departed from CityA at 4:0.
Traveled 200 miles to CityB, arrived at 0:0. Travel time, 0.0 hours.
Но мне нужен выход выглядеть следующим образом:
Please enter your destination travel start time (HH:MM): 05:30
Please enter your destination travel end time (HH:MM): 07:45
Please enter your home travel start time (HH:MM): 06:15
Please enter your home travel end time (HH:MM): 08:30
Departed from CityA at 05:30.
Traveled 100 miles to CityB, arrived at
07:45. Travel time, 2.25 hours.
Не могли бы вы объяснить причину «cin.clear () "каждый раз после cin >>? Благодарю. –
@PengZhang: прочитайте это: http://stackoverflow.com/questions/5131647/why-would-we-call-cin-clear-and-cin-ignore-after-reading-input –
Итак, это просто безопасно ? учитывая, что этот фрагмент кода сильно взаимодействует с пользовательским вводом. –