Hangman

Hi! I need some help:
I’m coding a hangman game but I’m struggling to figure out how to make so you
can guess individual letters,
any ideas? here’s the code for reference:

#include <iostream>
#include <string>
using namespace std;
string player1,player2,word,underscore,guess,clue1,clue2,clue3,clue4,clue5;
int wrong=0;
int main (){
string copy = word;

cout << "----------------------Hello! Welcome to the HANGMAN game!----------  ----------" << endl;
cout << "Please type in your name, PLAYER 1" << endl;
cin >> player1;
cout << "Please type in your name, PLAYER 2" << endl;
cin >> player2;
cout << "OK " << player1 << " and " << player2 << ". Let's start with the game!" << endl;
cout << player1 << " please input the word you want " << player2 << " to guess." << endl;

cin >> word;
cout << "\n";

cout << "Please Input A Clue for " << player2 << ": (NOTE: if you want to add a space between words use an underscore anything else results in an error.)\n";
cout << ">";
cin >> clue1;
cout << "\n";

cout << "Please Input Another Clue\n";
cout << ">";
cin >> clue2;
cout << "\n";

cout << "and another\n";
cout << ">";
cin >> clue3;
cout << "\n";

cout << "and another clue\n";
cout << ">";
cin >> clue4;
cout << "\n";

cout << "One more!\n";
cout << ">";
cin >> clue5;
cout << "\n";
//space
for (int x=0; x<30; x++){
cout << endl;
}

//UNDERSCORE
while (underscore.size() != word.size()){
underscore.push_back('-');}

cout << underscore << endl;

//MAIN WHILE
while(wrong<12){
cin >> guess;

//IF GUESS ISNT LETTER
if(guess.size() > 1){
if(guess==word){
cout << "Thats the right word." << endl;
break;
}
else{
cout << underscore << endl;
cout << "Wrong word try again." << endl;
cout << "Used: " << guess << endl;
wrong ++;
}

    }





if(underscore == word){
  cout << "You win!" << endl;
  break;
}



if(wrong==1){
cout << "|" << endl;
cout << "Clue: " << clue1 << endl;
}
else if(wrong==2){
cout << "|" << endl;
cout << "|" << endl;
}
else if(wrong==3){
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
}
else if(wrong==4){
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "Clue: " << clue2 << endl;
}
else if(wrong==5){
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
}
else if(wrong==6){
cout << "|---" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
}
else if(wrong==7){
cout << "|---" << endl;
cout << "|  O" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "Clue: " << clue3 << endl;
}
else if(wrong==8){
cout << "|---" << endl;
cout << "|  O" << endl;
cout << "|  |" << endl;
cout << "|" << endl;
cout << "|" << endl;
}
else if(wrong==9){
cout << "|---" << endl;
cout << "|  O" << endl;
cout << "| /|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "Clue: " << clue4 << endl;
}
else if(wrong==10){
cout << "|---" << endl;
cout << "|  O" << endl;
cout << "| /|\\" << endl;
cout << "|" << endl;
cout << "|" << endl;
}
else if(wrong==11){
cout << "|---" << endl;
cout << "|  O" << endl;
cout << "| /|\\" << endl;
cout << "| /"  << endl;
cout << "|" << endl;
cout << "Last Clue: " << clue5 << endl;
}
else if(wrong==12){
cout << "|---" << endl;
cout << "|  O" << endl;
cout << "| /|\\" << endl;
cout << "| / \\ "<< endl;
cout << "|" << endl;
cout << "YOU ARE DEAD\n";

cout << "Game over! The word was: " << word <<endl;
break;
}
}
}

Solved it! Don’t worry! oh! If you’re curious here’s the full code:

Full code

Hello, this is Gulshan Negi
Well, there are some syntax errors, you can copy the below correct code in your project.


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

string player1, player2, word, underscore, guess, clue1, clue2, clue3, clue4, clue5;
int wrong = 0;

int main() {
    string copy = word;

    cout << "----------------------Hello! Welcome to the HANGMAN game!----------------------" << endl;
    cout << "Please type in your name, PLAYER 1" << endl;
    cin >> player1;
    cout << "Please type in your name, PLAYER 2" << endl;
    cin >> player2;
    cout << "OK " << player1 << " and " << player2 << ". Let's start with the game!" << endl;
    cout << player1 << " please input the word you want " << player2 << " to guess." << endl;

    cin >> word;
    cout << "\n";

    cout << "Please Input A Clue for " << player2 << ": (NOTE: if you want to add a space between words use an underscore; anything else results in an error.)\n";
    cout << ">";
    cin >> clue1;
    cout << "\n";

    cout << "Please Input Another Clue\n";
    cout << ">";
    cin >> clue2;
    cout << "\n";

    cout << "and another\n";
    cout << ">";
    cin >> clue3;
    cout << "\n";

    cout << "and another clue\n";
    cout << ">";
    cin >> clue4;
    cout << "\n";

    cout << "One more!\n";
    cout << ">";
    cin >> clue5;
    cout << "\n";

    // Space
    for (int x = 0; x < 30; x++) {
        cout << endl;
    }

    // UNDERSCORE
    underscore.assign(word.size(), '-');
    cout << underscore << endl;

    // MAIN WHILE
    while (wrong < 12) {
        cin >> guess;

        // IF GUESS ISN'T A LETTER
        if (guess.size() > 1) {
            if (guess == word) {
                cout << "That's the right word." << endl;
                break;
            } else {
                cout << underscore << endl;
                cout << "Wrong word, try again." << endl;
                cout << "Used: " << guess << endl;
                wrong++;
            }
        }

        if (underscore == word) {
            cout << "You win!" << endl;
            break;
        }

        if (wrong == 1) {
            cout << "|" << endl;
            cout << "Clue: " << clue1 << endl;
        } else if (wrong == 2) {
            cout << "|" << endl;
            cout << "|" << endl;
        } else if (wrong == 3) {
            cout << "|" << endl;
            cout << "|" << endl;
            cout << "|" << endl;
        } else if (wrong == 4) {
            cout << "|" << endl;
            cout << "|" << endl;
            cout << "|" << endl;
            cout << "|" << endl;
            cout << "Clue: " << clue2 << endl;
        } else if (wrong == 5) {
            cout << "|" << endl;
            cout << "|" << endl;
            cout << "|" << endl;
            cout << "|" << endl;
            cout << "|" << endl;
        } else if (wrong == 6) {
            cout << "|---" << endl;
            cout << "|" << endl;
            cout << "|" << endl;
            cout << "|" << endl

I am sure it will help you.
Thanks

1 Like