2014-02-19 48 views
0

我到目前为止有:如何及时读取输入并格式化输出?

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

既然你想用户在相关提示的同一行上输入,根据提示排除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; 
+0

你能解释一下“cin.clear的原因( )“每次后cin >>?谢谢。 –

+0

@彭章:阅读此:http://stackoverflow.com/questions/5131647/why-would-we-call-cin-clear-and-cin-ignore-after-reading-input –

+0

因此,它只是为了safefy ?考虑到这段代码与用户输入大量交互。 –

1

你得到错误的结果这种情况的原因:

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

贝娄是你的程序的修订版:

#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.