I am making a wheel of fortune game and have successfully figured out how to change the word that you're trying to guess into astricks *. However, I cannot figure out how to change it back once the user inputs a correct letter.

The puzzle comes from the user inputting a file name previously in the code.

Here is my code for this segment so far:

for (int counter = 0; counter < phrase.length(); counter++)
               {
                       if (phrase[counter] = puzzle[counter])
                               puzzle[counter] = phrase[counter];

                        cout << "PUZZLE: " << phrase << endl; // for debugging purposes
               }

                        cout << "PUZZLE: " << phrase << endl;  // for debugging purposes

Can you please help clear up for me where I am going wrong?

Thank you so much for your time.

Dani AI

Generated

Short summary and a compact, reusable approach.

had the right idea: keep the original phrase and a separate "puzzle" string that shows '*' for letters and the real characters for spaces/punctuation. correctly pointed out the classic bug (using = instead of ==) that turns a comparison into an assignment. The clean flow is: initialize puzzle, validate a single-letter guess, scan the original phrase and reveal any matching positions in puzzle, then update the display.

A minimal, safe implementation pattern:

void initPuzzle(const std::string &phrase, std::string &puzzle) {
    puzzle.resize(phrase.size());
    for (size_t i = 0; i < phrase.size(); ++i) {
        char c = phrase[i];
        puzzle[i] = (std::isalpha(static_cast<unsigned char>(c)) ? '*' : c);
    }
}

bool revealGuess(const std::string &phrase, std::string &puzzle, char guess) {
    bool found = false;
    guess = std::toupper(static_cast<unsigned char>(guess));
    for (size_t i = 0; i < phrase.size(); ++i) {
        if (std::toupper(static_cast<unsigned char>(phrase[i])) == guess) {
            if (puzzle[i] == '*') {
                puzzle[i] = phrase[i]; // reveal original case
                found = true;
            }
        }
    }
    return found;
}

Troubleshooting notes and best practices: validate that input is a single alphabetic character; keep a set of guessed letters to ignore repeats; reveal non-alpha characters at init so spaces and punctuation are visible; always print the puzzle string (not the original phrase) for the player view; use size_t for indices; and cast chars to unsigned char before calling std::isalpha/std::toupper to avoid undefined behavior. For and anyone splitting UI and networking, make the reveal/init logic part of a small, dependency-free module so both client and server can reuse it.

Recommended Answers

All 5 Replies

You should use two string variables: in one of them you should keep that word and in the other word changed into combinations of '*' and latters. Your code probably fail there: "if (phrase[counter] = puzzle[counter])" becouse you should use == instead of =.

Thank you for your help. That is one of my errors.

I was told to do this, " you need to compare what the user entered as a letter choice to each index of the phrase to determine if it needs to be unhidden (changed from astrick to letter) in the puzzle array" but I'm not sure how to do that. any suggestions?

Use a loop and check are the choosen letter and ith element of the word the same. If they are unhide ith element in the puzzle.

I got it! Thank you so much for your help!

Member Avatar for Member #887313

I am also working on a Wheel of Fortune game. My version lets you play against a friend across the internet. I have the internet component working, and an just starting the UI. I would share the internet code with you if you'd like to share your UI code with me. What do you think?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.