2013-01-04 58 views
1

我正在努力爭取爲我的遊戲工作的輸入驗證。遊戲的C++輸入驗證

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

int main() 
{ 

    int iGumballs; 
    int iUserguess; 
    int iGuesses = 0; 

    while (true) 
    { 
     system("CLS"); 
     cin.clear(); 
     iGuesses = 0; 

    srand(static_cast<unsigned int>(time(0))); 
    iGumballs = rand()%1000+1; 
    cout << "How many gumballs are in the bumball jar? You guess! 1-1000" << endl; 

    do 
    { 
     cout << "Enter your guess: "; 


    cin >> iUserguess; 


    if(iUserguess > iGumballs) 
    { 
     cout << "Too high!" << endl << endl; 
    } 

    if(iUserguess < iGumballs) 
    { 
     cout << "Too low!" << endl << endl; 
    } 
    iGuesses ++; 
    } 
    while(iUserguess > iGumballs || iUserguess < iGumballs); 
    cout << "You guessed the right amout of gumballs" << endl << endl; 
    cout << "You took " << iGuesses << " guesses" << endl << endl; 
    system ("pause"); 
    } 
    return 0; 
} 

我基本上希望程序當用戶輸入小於1的數,和高於1000顯示

Your Guess: Sorry, incorrect input - try again 

,以及某種驗證這確保輸入一個數而不是一個字母或符號。我嘗試過cin.fail(),但我無法完成它的工作。

感謝, 約翰

回答

1

呦可以使用if(cin)來檢查輸入流的狀態,因爲操作符>>將返回傳遞給它的輸入流,您可以使用if(cin>>iUserguess)

如果cin處於失敗狀態 - 可能是因爲用戶輸入了非數字 - 表達式if(cin>>iUserguess)將評估爲false。

如果用戶輸入一個非數字,則需要在呼叫cin.clear()以清除流狀態,並且cin.ignore()放棄輸入,然後嘗試再次讀取一個數字。

所以使用你的榜樣,它可以改變這樣:

#include <iostream> 
#include <ctime> 
#include <limits> 
using namespace std; 

int main() 
{ 

    int iGumballs; 
    int iUserguess; 
    int iGuesses = 0; 

    while (true) 
    { 
     system("CLS"); 

     iGuesses = 0; 

     srand(static_cast<unsigned int>(time(0))); 
     iGumballs = rand()%1000+1; 
     cout << "How many gumballs are in the bumball jar? You guess! 1-1000" << endl; 

     do 
     { 
      cout << "Enter your guess: "; 
      if(cin >> iUserguess) 
      { 
       iGuesses ++; 
       if(iUserguess > iGumballs) 
       { 
        cout << "Too high!" << endl << endl; 
        continue; 
       } 

       if(iUserguess < iGumballs) 
       { 
        cout << "Too low!" << endl << endl; 
        continue; 
       } 
      } 
      else 
      { 
       cout<<"incorrect input - try again\n\n"; 
       cin.clear(); 
       cin.ignore(numeric_limits<streamsize>::max(),'\n'); 
       continue; 
      } 
     } 
     while(iUserguess > iGumballs || iUserguess < iGumballs); 
     cout << "You guessed the right amout of gumballs" << endl << endl; 
     cout << "You took " << iGuesses << " guesses" << endl << endl; 
     system ("pause"); 
    } 
    return 0; 
} 
+0

它接受以數字開頭的值(例如0xxx,「Too low!」輸出)。 – coelhudo

+0

然後使用字符串解決方案會更好。閱讀你的輸入作爲字符串,並做你需要的任何檢查,在@ coelhudo的例子 –

+0

嗯好,謝謝所有的幫助人。 –

2

您將需要一些測試,看看是否是一個數字或沒有,試試這個:

#include <iostream> 
#include <string> 
#include <ctime> 
#include <boost/lexical_cast.hpp> //dependency that can be optional 
using namespace std; 

bool is_number(const std::string& s) 
{ 
    std::string::const_iterator it = s.begin(); 
    while (it != s.end() && std::isdigit(*it)) ++it; 
    return !s.empty() && it == s.end(); 
} 

int main() 
{ 

    int iGumballs; 
    std::string iUserguessStr; 
    int iUserguess; 
    int iGuesses = 0; 

    while (true) 
    { 
     system("CLS"); 
     cin.clear(); 
     iGuesses = 0; 

    srand(static_cast<unsigned int>(time(0))); 
    iGumballs = rand()%1000+1; 
    cout << "How many gumballs are in the bumball jar? You guess! 1-1000" << endl; 

    do 
    { 
     cout << "Enter your guess: "; 


    cin >> iUserguessStr; 
    if(is_number(iUserguessStr)) 
     iUserguess = boost::lexical_cast<int>(iUserguessStr); //you can make your own or maybe use lexical cast to transform a string into integer 
    else 
     continue; //put some fancy message here warning the user 


    if(iUserguess > iGumballs) 
    { 
     cout << "Too high!" << endl << endl; 
    } 

    if(iUserguess < iGumballs) 
    { 
     cout << "Too low!" << endl << endl; 
    } 
    iGuesses ++; 
    } 
    while(iUserguess > iGumballs || iUserguess < iGumballs); 
    cout << "You guessed the right amout of gumballs" << endl << endl; 
    cout << "You took " << iGuesses << " guesses" << endl << endl; 
    system ("pause"); 
    } 
    return 0; 
} 

我的答案是基於一個相關問題:How to determine if a string is a number with C++?

+0

明天我會下載boost儲存庫,看看你的解決方案。 –

+0

使用boost可以安全地檢測轉換錯誤(捕獲boost :: bad_casting異常),但是您可以嘗試標準函數以在c字符串和名爲std :: atoi(http:// en。)的整數之間進行轉換。 cppreference.com/w/cpp/string/byte/atoi) – coelhudo

1

要驗證字符,你可以使用try-catch結構。

- 首先讀入字符串並嘗試Typecasting it並用try-catch處理錯誤。
- 然後使用條件確保輸入處於範圍內。
- 如果輸入無效,您可以編寫一條錯誤消息來顯示。

+0

使用try-catch控制程序的流程通常不是一個好主意。 try-catch用於異常處理。異常處理很慢。使用異常並沒有錯,但是首先要手動檢查錯誤。不要拋出不必要的例外。 – Skalli