2016-06-10 64 views
0

我是編程新手,需要項目幫助。我製作了一個模擬酒店預訂的程序,問題是每當爲第一個問題輸入一個非整數時,它就會陷入無限循環。如果您遇到第二個問題並輸入一個非整數,它會通過刪除小數點後的任何內容來接受整數,然後跳過下一個問題並停止運行該程序。如何在C++中只接受整數

#include <iostream> 
    #include <string> 
    using namespace std; 

int stay_length (int stay) 
{  
    int nights = stay; 
    int total = 0*nights; 
    return total; 
} 

int rooms_booking(int rooms) 
{ 
    int rooms_booked = rooms; 
    int total = 0; 

    if(rooms_booked > 0) 
    { 
     total = rooms_booked * 50; 
    } 
    else 
    { 
     total = 0; 
    } 
    return total; 
} 

int main(){ 
    int x; 
    string repeat; 
    int nights; 
    int total = 0; 
    int rooms_avail = 10; 
    int rooms; 

    cout << "Hello! Welcome to Hotel COMP 150." << endl; 
    do { 
     if (rooms_avail > 0) { 
     cout << "How many nights will you be staying?" << endl; 

    } 
    else { 
     cout << "Sorry, but there are no rooms available. " << endl; 
    } 
     do { 
     cin >> nights; 


      if (nights > 0 && nights <= 28) 
      { 
       int total1 = stay_length(nights); 
       cout << "You are staying for " << nights << " nights" << endl; 
       cout << "That costs: $" << total1 << endl; 
       total = total + total1; 
      } 
      else 
      { 
      cout << "You cannot stay less than 1 or more than 28 nights" << endl; 
      } 

     } while (nights <= 0 || nights >28); 

      if (rooms_avail > 0) 
      { 
       cout << "How many rooms will you be booking?" << endl; 
        cout << "There are " << rooms_avail << " available." << endl; 
        cin >> rooms; 

        if (rooms > 0 && rooms <= rooms_avail) 
        { 
         int total2 = rooms_booking(rooms); 
         cout << "You are booking " << rooms << " rooms." << endl; 
         cout << "That costs : $" << total2 << endl; 
         total = total + total2; 
         rooms_avail = rooms_avail - rooms; 
        } 
        else if (rooms <= 0 || rooms > rooms_avail) 
        { 
         do{ 
          cout << "You can only book a minimum of 1 room or a maximum of " << rooms_avail << endl; 
          cin >> rooms; 
          } while (rooms <= 0 || rooms > rooms_avail); 
          int total2 = rooms_booking(rooms); 
          cout << "You are booking " << rooms << " rooms." << endl; 
          cout << "That costs : $" << total2 << endl; 
          total = total + total2; 
          rooms_avail = rooms_avail - rooms; 
        } 
        else 
        { 
        cout << "You cannot book more than " << rooms_avail << endl; 
        } 

      } 

      else 
      { 
       cout << "Sorry, all rooms have been booked." << endl; 
      } 
    cout << "Your total so far is: $" << total << endl; 
    cout << "Would you like to make another booking? Enter 'Y' or 'y' to do so." << endl; 
    cin >> repeat; 

     }while(repeat == "Y" || repeat == "y"); 
    return 0; 
} 
+4

這是很好的,你提供的代碼,但考慮哪些代碼「最小」的量將表現出問題。 [這也許值得一讀](http://sscce.org/)。您的預訂或費用問題?如果沒有,請考慮如何刪除它,並仍然幫助解決給您帶來麻煩的問題。 – HostileFork

+1

請同時縮進 - 這很難遵循。 – rrauenza

回答

1

它總是更好地使用std::getline()而不是operator>>std::cin閱讀交互式輸入。

operator>>不適用於從標準輸入中讀取單行文本並進行存儲。這就是std::getline()的用途。

如果輸入了錯誤的類型的輸入,而不是什麼operator>>預計,它設置std::cin到一個失敗的國家,這使得未來所有試圖讀取std::cin立即失敗,導致你所觀察的無限循環。

爲了正確地做到這一點,這將是兩種:

1)每operator>>後務必檢查是否fail(),看看如果輸入失敗,如果是從錯誤與clear()恢復,然後ignore()。這很快就會變老。

2)用std::getline()來讀取單行文本比較容易,然後自己解析輸入。如果您願意,可以構建一個std::istringstream,如果這樣可以更容易使用operator>>

+0

*「最好使用'std :: getline()'而不是'operator >>'」* - * always *是一個在這裏使用的頂級單詞,作爲一般性陳述非常錯誤;你繼續討論爲用戶提供另一個機會,這確實是你的「總是」開始看起來可信的情況,但是對於操作符>>有一些微妙的好處,比如幫助測試/腳本化調用,回聲值1的值2 value3 |程序「,它可以通過另一種方式使平衡倒退,一般情況下 - 不希望區分空格和換行符,或者更喜歡簡潔或更快的情況。 –

0

可以實現基本的用戶輸入錯誤通過控制檯檢查與這些方針的東西:

int nights = 0; 
// error checking loop 
while(1) { 
    std::cout << "How many nights will you be staying?" << endl; 
    std::cin >> nights; 
    // input valid 
    if(!std::cin.fail() && (std::cin.peek() == EOF || std::cin.peek() == '\n') 
     && nights > 0 && nights <= 28) { 
     // do stuff 
     break; // break from while loop 
    } 
    // input invalid 
    else { 
     std::cin.clear(); 
     std::cin.ignore(256, '\n'); 
     std::cout << "An input error occurred." << std::endl; 
    } 
} 
+0

這將旋轉打印「發生輸入錯誤。」如果有人做了類似「echo three | the_program」;更好地終止EOF。 –

相關問題