C++ Wordguess game (updated) with minor problems?

  boolean, c++, for-loop, if-statement, while-loop

I’m making a Word Guess game. I posted it yesterday, but I’ve made progress and wanted to give an updated version:

RULES:

  • Player1 will enter a word of any length for Player 2 to guess. [DONE]

  • Player 2 will have 5 letter guesses and 5 word guesses total. [DONE]

  • Ignore case; ex. If user enter ‘T’ display it as lower (without using toupper/tolower)[STUCK]

  • If user guesses correct letter, replace dash(es) with letter, and what position; ex: "testing" and Player2 enters ‘T’ then output should be:

    "t _ _t _ _ _" 
    
    You guessed the letters at 1 and 4 correct letter guess correct. You have 4 letter guesses remaining. Enter your first word guess".
    

PROBLEMS:

  • Program doesn’t ignore case for some reason [need to fix]
  • If the letterguess is correct, or incorrect, count it as 1 letter guess and have them attempt their first word guess. DO not worry about a user guessing letters multiple times, just count it as a used guess.
  • If I run the word: "testing" and I enter "T" it should display "You guessed the correct letter at 1 and 4. Enter word guess" / instead if I enter ‘T’ it displays "Wrong letter"…. if I enter ‘t’ it only reads the first t and not the fourth.
  • After each letter guess (whether right or wrong) give the user a chance to guess the word; if the wordguess is correct, then game over. If the word is not correct, enter second letterguess (repeat 5 times for each until word guess, or run out of guesses)

Any help appreciated. I will post code below, as well as a hardcoded screenshot of what my code should look like if user guesses word on first wordguess:

enter image description here

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

int main()
{
    string word, guessword, guessletter;
    string dashes = string(word.length(), '_');
    int numletterguess = 5;
    int numwordguess = 5;
    bool gameover = false;
    bool validletterguess;
    bool validwordguess;

    cout << "Player 1. Enter a word for Player 2 to guess: " << endl;
    getline(cin, word);

    dashes = string(word.length(), '_');

    while (gameover == false && numletterguess > 0 && numwordguess > 0) // start game Round 1. 
    {
        guessletter = "";
        cout << "Player 2, you have " << numletterguess << " letter guesses left and " << numwordguess << " word guesses left. " << endl;
        cout << "Enter your letter guess: " << endl;
        cin >> guessletter; //letter guess

        while (guessletter.length() != 1)  // if the letter guess is more than a single letter, loop until user enters 1 letter.
        {
            cout << "Enter only 1 Letter. " << endl;
            cout << "Try again: " << endl;
            cin >> guessletter;

        }
        numletterguess--;
        validletterguess = false;
        for (int i = 0; i < word.length(); i++) //ignore case
        {
            if (guessletter.at(0) == word.at(i))
            {
                cout << "You guessed a letter correct at position " << i + 1 << " of the word" << endl;
                dashes.at(i) = word.at(i);
                cout << dashes << endl;
                validletterguess = true;
                break;
            }
            else {
                cout << "Wrong letter guessed" << endl;
                cout << "Player 2, you have " << numletterguess << " letter guesses left" << endl;
                cout << "Enter your wordguess" << endl;
                cin >> guessword;
                if (guessword == word) {
                    gameover = true;
                    break;
                }
                else {
                    numwordguess--;
                    cout << "You guessed the wrong word!" << endl;
                    cout << "You have " << numwordguess << " guess left" << endl;
                    continue;
                }

             }       
        }
    }
    if (gameover == true)
    {
        cout << "You have beaten the game!" << endl;
    }
    else cout << "Game Over!" << endl;

}

Source: Windows Questions C++

LEAVE A COMMENT