Warning: I am brand new to programing. I am trying to create a random letter generator game for a class project. I feel like I have a good start to it but I am having difficulty with a few points. The program is supposed to ask the player how many games they would like to play. The maximum number of guesses they get per game is 5 and then it is supposed to print out what the correct answer was if it was not guessed. As it is I have it so that it will run the correct number of guesses but not games and it dosent cout<< the correct answer when all guesses are done. Any help is appreciated, thanks.

#include<iostream>;
#include<cstdlib>;
#include<ctime>;
using namespace std;
int main()
{
    char alphabet [27];
    int number_of_games;
    char guess;
    int x = 1;
    srand(time(0));
    int n = rand() % 26 + 1;  

        cout<<"Weclome to the Letter Guessing game!\n";
        cout<<"You have 5 chances to guess each letter.\n \n";
        cout<<"How many games do you want to play?\n";
        cin >> number_of_games; 

    while (x <= number_of_games) //Need to get it for how many rounds, not how many guesses
    {
        //cout << (char)(n+97); //cheat to make sure working
        cout<<"Enter your guess: ";
        cin >> guess;
        int guessValue = int(guess);

        if (guessValue > (n+97))
        {
            cout<<"The letter you are trying to guess is before " <<guess <<"\n"; 
        }
        else if (guessValue < (n+97))
        {
            cout<<"The letter you are trying to guess is after " <<guess << "\n";
        }
        else if(guessValue ! (n+97) || (guessValue !=){
            cout << "The answer you were looking for was " << (n+97) << "\n";
            //unsure about this section. attempt to cout the correct answer
            }
        else
        {
            cout<<"Your guess is correct! \n";
            break;
        }
        //if answer is not right after x tries, cout the correct answer
        x++;
    }

    system("pause");
    return 0;
}

Recommended Answers

All 4 Replies

it dosent cout<< the correct answer when all guesses are done.

Edited.

Your loop looks incorrect . That is, you need to check your code structure and install the missing loop for that.

Tinstaafl is right here. The names of the variables threw me off here. Rewrite that and check your program flow.

This is a very good argument for using meaningful names for your variables instead of just letters. You set x to the number of games, but at the bottom of the while loop you indicate it should be the number of guesses.

The first thing I would suggest is to go through your code and change your variable names to more meaningful names. This should make it easier to see where the flow of your code is wrong.

The second thing to look at, is the variable for the answer. if you make it a char in the declaration you can just use guess and you don't have to make an addition to it each time you check a guess - char answer = (char)((rand() % 26 + 1) + 'a');

commented: Thank you for your help! +0

What tinstaafl said. Compilers turn all variable names into symbols anyway. So using X vs. Games doesn't make any difference to the size or performance of your code, but it DOES make it a lot easier to debug!

You should also consider if the user enters a capital letter as well. The toupper and tolower functions in <cctype> can be useful for this.

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.