2013-11-25 86 views
0

我正在爲我的C++類中的最後一個hangman項目工作。在我的hang子手遊戲中,只要玩家不斷收到正確的信件,他們輪到他們就會無限期地持續下去,直到獲得這個字。此外,爲了避免一遍又一遍地使用相同的字母,我包括一個循環和一個布爾數組,當選擇一個字母時它將被設置爲true。我的問題是,我試圖讓這個遊戲儘可能的簡單,而且當我嘗試輸入多個字符時,程序會通過前幾個字符並忽略其他字符。遇到這個問題後,我決定讓用戶一次只能輸入一個字符,如果他們再次進入,他們會被忽略。所以我想知道是否有一種方法可以讓程序只接受一次用戶的一個字符。忽略C++中的字符

這裏是什麼,從我的程序一圈貌似

#include <iostream> 
#include <fstream> 
#include <string> 
#include <cstring> 
using namespace std; 
int main{ 
    char choword []='hello' 
    char alpha[26] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 
       'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 
       's', 't', 'u', 'v', 'w', 'x', 'y', 'z' }; 
    int counter=0; 
    int tries=6; 
    bool rightw; 
    bool usedw; 
    bool gamew; 
    bool gameo; 
    bool lguess 
    char ch; 
    bool conturn=true; 
    gameo=false; 
    gamew=false; 
    bool conturn=true; 
    tries=6; 

    const unsigned int leng=strlen(choword); 
    bool* rightw= new bool[leng]; 
    for (unsigned int index =0;index<leng;index++){ 

     rightw[index]=false; 
     cout<<lright[index]; 
    } 

    bool* usedw= new bool[26]; 
    for(int counter11=0;counter11<26;counter11++){ 
     usedw[counter11]=false; 
    } 
      while(conturn==ture){ 
        lguess=false; 
        for(unsigned int index2=0;index2<leng;++index2){ 
       if(rightw[index2]==true){ 
        cout<<choword[index2]; 

       } 
       else{ 
        cout<<"*"; 

       } 


      } 
      cout<<"\n"; 
      for(int counter=0;counter<26;counter++){ 
       if(usedw[counter]==true){ 
        cout<<alpha[counter]; 
       } 
      } 
      cout<<"\n"; 
      cout<<"Pick a letter and guess the word player."<<endl; 
      cout<<"Please only select one letter at a time"<<endl; 
      cin>>ch; 

      ch=tolower(ch); 

      while(ch<97||ch>122){ 
       cout<<"You used the wrong type of character try again."<<endl; 
       cin>>ch; 
       ch=tolower(ch); 

      } 
      while(counter<26){ 
       if(ch==alpha[counter]&&usedw[counter]==false){ 
        usedw[counter]=true; 
        break; 
       } 
       if(ch==alpha[counter]&&usedw[counter]==true){ 
        cout<<"This letter has alread been used please enter another letter"<<endl; 
        cin>>ch; 
        while(ch<97||ch>122){ 
         cout<<"You used the wrong type of character try again."<<endl; 
         cin>>ch; 
         ch=tolower(ch); 

        } 
        counter=0; 
       } 
       else{ 
        counter=counter+1; 
       } 
      } 
      counter=0; 
      for (unsigned int index3=0;index3<leng;++index3){ 
       if (ch==choword[index3]){ 
        rightw[index3]=true; 
        lguess=true; 
        winning=winning+1; 
       } 


      } 
      if (lguess==false){ 
       tries=tries-1; 
       vonturn=false; 
      } 
      lguess=false; 
      if (winning==strlen(choword)){ 
       onaroll=false; 
      } 
      } 
} 

的簡化版本,記住這只是截至第一玩家的回合東西,如刪除動態數組到底是在我的主程序。

回答

0

爲了讓用戶只輸入一個字符的時間,你可以讀取輸入爲std::string,並檢查其lenght(I防腐的通道名稱,在你的代碼,這是相同的變量):

string input; 
char ch; 
cin>>input; 
if(input.size()>1){ 
    //handle mistake 
} 
ch = input[0]; 

關於處理用戶的錯誤,可能是最好的選擇是再次要求輸入用戶:

string input; 
char ch; 
cin>>input; 
while(input.size() != 1){ 
    cout<<"Enter a letter, please!\n"; 
    cin>>input; 
} 
ch = input[0]; 
+0

我忘了說我是一種仍然是一個初學者我將如何處理錯誤 – user2340686

+0

我增加了一種方法來處理我的答案的錯誤。我希望這已經足夠清楚,它可以幫助你。 – Paul92