到目前为止我所拥有的:
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.
答案 0 :(得分:2)
由于您希望用户输入与相关提示位于同一行,因此请在提示的后面删除endl
。您还必须忽略用户输入的换行符,并考虑:
个字符。
请改为尝试:
#include <iostream>
#include <string>
#include <iomanip>
#include <string>
using namespace std;
...
int dest_startHOUR, dest_startMIN;
int dest_endHOUR, dest_endMIN;
int home_startHOUR, home_startMIN;
int home_endHOUR, home_endMIN;
int dest_miles;
string home_city, dest_city;
char c;
...
cout << "Please enter your destination travel start time (HH:MM): ";
cin >> dest_startHOUR >> c >> dest_startMIN;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Please enter your destination travel end time (HH:MM): ";
cin >> dest_endHOUR >> c >> dest_endMIN;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Please enter your home travel start time (HH:MM): ";
cin >> home_startHOUR >> c >> home_startMIN;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Please enter your home travel end time (HH:MM): ";
cin >> home_endHOUR >> c >> home_endMIN;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << endl;
cout << "Departed from " << home_city << " at " << setw(2) << setfill('0') << dest_startHOUR << ":" << dest_startMIN << "." << endl;
cout << "Traveled " << dest_miles << " miles to " << dest_city << ", arrived at " << setw(2) << setfill('0') << dest_endHOUR << ":" << dest_endMIN << ". " << endl;
cout << "Travel time, " << dest_endHOUR - home_startHOUR << "." << (dest_endMIN - home_startMIN) / 60 << " hours." << endl;
cout << endl;
答案 1 :(得分:1)
由于这个原因你得错了结果:
Bellow是您的计划的修订版本:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int dest_startHOUR, dest_startMIN;
int dest_endHOUR, dest_endMIN;
int home_startHOUR, home_startMIN;
int home_endHOUR, home_endMIN;
int dest_miles = 200;
string home_city = "CityA", dest_city = "CityB";
char ch;
cout << "Please enter your destination travel start time (HH:MM): ";
cin >> dest_startHOUR >> ch >> dest_startMIN;
cout << "Please enter your destination travel end time (HH:MM): ";
cin >> dest_endHOUR >> ch >> dest_endMIN;
cout << "Please enter your home travel start time (HH:MM): ";
cin >> home_startHOUR >> ch >> home_startMIN;
cout << "Please enter your home travel end time (HH:MM): ";
cin >> home_endHOUR >> ch >> 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 - dest_startHOUR << "." << (dest_endMIN - dest_startMIN) / 60.0 * 100
<< " hours." << endl << endl;
return 0;
}
在这里,您可以找到修订程序的运行示例:
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 5:30.
Traveled 200 miles to CityB, arrived at 7:45. Travel time, 2.25 hours.