2013-11-25 84 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