0

I'm working on a hangman project for my final in my c++ class. In my game of hangman as long as a player keeps getting the right letters they'll their turn will keep going indefinitely until the get the word. Also in order to avoid the same letter being used over and over I included a loop that and a bool array that will be set to true when a letter is selected. My problem is I'm trying to make this game as fool proof as possible and I when I try entering in multiple characters the program will go through the first few characters and ignore the rest. After coming across this problem I decided to make so the user could only enter in one character at a time and if they entered anymore they would be ignored. So I was wondering if there was a way to make it so the program only accepts one character from the user at a time.

Here is a simplified version of what one turn from my program looks like

#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;
            }
           }
}

keep in mind this is only up till the end of the first player's turn stuff like deleting the dynamic arrays is in my main program.

user2340686
  • 97
  • 1
  • 3
  • 11

1 Answers1

0

In order to allow user to input just a character at a time, you can read input as a std::string and check for its lenght (I preserved ch name, as in your code; it is the same variable):

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

About handling the user's mistake, probably the best option is to ask the user for input again:

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