如何读取输入并格式化输出?

时间:2014-02-19 03:37:30

标签: c++ datetime

到目前为止我所拥有的:

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.

2 个答案:

答案 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)

由于这个原因你得错了结果:

  • 在每个提示消息之后放置 endl ,这会使光标移动到下一行,然后才能收到用户的响应。
  • 当cin&gt;&gt; dest_startHOUR&gt;&gt; dest_startMIN;执行后,您的程序需要来自用户的两个整数变量(即小时和分钟)。但是,在用户输入冒号后,程序会将其视为分钟的值。因此,您的程序执行先于并且给您不正确的结果。
  • 您应该(dest_endHOUR - dest_startHOUR)计算到目的地的出发时间,而不是(dest_endHOUR - home_startHOUR)。
  • 要获得到达目的地的出发旅行时间的一小时,您应该将表达式更改为((dest_endMIN - dest_startMIN)/ 60.0 * 100)。您需要在60之后放置.0以避免整数除法,并且需要将结果乘以100以将其转换为小时的一小部分。但是,以HH:MM格式打印出旅行时间是有意义的。
  • 如果您只考虑24小时格式并且行程在早上开始并在当天晚上结束,那么您的程序可以正常工作。否则,您需要增强代码才能考虑此问题。

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.