2014-02-19 96 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.