I'm new at this. But I'm taking a class and I need to turn this in by Friday. Please help me. I can only use strings and arrays.
My hangman program needs five things:
1. user is prompted for a word that is ten letters or less, or else the program rejects it and asks for reentry.
2. if the word is too long or has a nonletter, the program rejects it and asks for reentry.
3. If the word has uppercase letters, it needs to be converted to lowercase
4. if there is more than 6 wrong guesses, the program ends.
5. If the user repeats a guess or enters a non letter, the program outputs a warning, but does not count it as a wrong guess.

So including those five things, the game is won when all the letters of the word is guessed.

Here's what I have so far:

#include <iostream>
#include <cctype>
#include <cstring>

using namespace std;

int main()
{
    bool guessed[26] = {false};
    char guess;
    char solution[11]; //the word that is inputted
    int wrong_count(0);

    cout << "Enter a word no more than ten letters: ";
    cin >> solution;


    while (true) //I tried writing && wrong_count <6, but it doesn't work.
    {
        cout << "Please enter your guess: " << endl;
        cin >> guess;
        cout << "You've entered the letter " << char(tolower(guess)) << endl; //converts uppercase into lowercase letters

        if (bool (guessed [guess - 'a'] == true)) //I think I need a loop here, but I don't know where to start
            cout << "You've guessed " << char(tolower(guess)) << " already." << endl;
        else
            guessed[guess - 'a'] = true;

        for(int i = 0; i < strlen(solution); i++)
        {
            if (guess == solution[i])
                cout << "correct" << endl;
            else
                wrong_count++;
        }
    }



    if (wrong_count ==6)
        cout << "You lose " << endl;






    return 0;
}

what parts of the program do you need help with? No, I'm not going to finish it for you.

Just complete the instructions one at a time. Look at the 1st instruction, does the program you posted do all that? Answer: No. So the first thing you need to do is to add code to the program to make it comply with instruction #1.

Instructions 1, 2 and 3 all apply to the word that is entered, not the guesses.

Thank you for replying. I don't expect anyone to finish this program for me. But I would like help.

I have a problem with the first one. I don't know how to check for the number of letters in the word. But I managed to write a function to get lowercase letters. I also need to write a function to see if the guessed letter is in the word, but I don't know how to start it.

I wrote a for loop for it, but I think I need a function to check if the letters guessed are in solution:

 for (int i; i < strlen; ++i)
 {
     if (guesss == solution)     
         cout << "Correct" << endl;
     else
         wrong_count++;
 }        

The whole code looks like this now:

void lowercase(char[]);         //function prototype 
void lowercase(char solution[]) //function definition
{
    for (int i = 0; i < strlen(solution); ++i)
        solution[i] = tolower(solution[i]);
}
int main()
{
    bool guessed[26] = {false};
    char guess;
    char solution[11];  //word that the user inputs in
    int wrong_count(0);

    cout << "Enter a word no more than ten letters: ";
    cin >> solution;
    lowercase(solution);
    cout << "The word is: " << solution << endl;


    while (wrong_count < 6)
    {
        cout << "Please enter your guess: " << endl;
        cin >> guess;
        cout << "You've entered the letter " << char(tolower(guess)) << endl;

        if (bool (guessed [guess - 'a'] == true))
            cout << "You've guessed " << char(tolower(guess)) << " already." << endl;
        else
            guessed[guess - 'a'] = true;

        for (int i; i < strlen; ++i)
        {
            if (guesss == solution)     //I think my bool expression is wrong here
                cout << "Correct" << endl;
            else
                wrong_count++;
        }        

    }

    if (wrong_count ==6)
        cout << "You lose" << endl;
    else 
    {
        cout << "You win" << endl;
        cout << "The word was " << solution << endl;
    }
    return 0;
}

you need to put lines 14 and 15 inside a loop. After line 15 call strlen() to get the length of the string. If the length is incorrect then loop back to line 14.

The function prototype on line 1 is not needed since it is defined in line 2. Function prototypes are only needed when a function is called before it has been defined.

line 11: The string should be a little larger than that to allow the user to enter a string of more than 10 characters, which can easily be done when using the cin >> operator. You can limit input to no more than 10 characters by using getline() instead of >>.

Thanks for the help. Would the loop be like:

while (false)
        {
            cout << "Enter a word no more than ten letters: ";
            cin.getline (solution, 10);
            if (strlen(solution) <= 10)
                return true;
            else
                return false;

I wrote another function to check if the guessed letter is in the solution. Also, I tried to write a function to see if all the letters have been guessed. But I got stuck. Here's what I wrote:

bool solved(char solution[], bool guessed[])
{
    for (int i = 0; i < strlen(solution); ++i)
    { if (guessed[solution[i] - 'a'] == false)
        return false;
      else
        //I'm not sure what to put here. Would is be true?

So here's what the code looks like now without the above code.

void lowercase(char solution[]) //function definition
{
    for (int i = 0; i < strlen(solution); ++i)
        solution[i] = tolower(solution[i]);
}

bool in(char guess, char solution[])
{
    for (int i = 0; i < strlen(solution); ++i)
        if (guess == solution[i])
            return true;
    return false;
}

int main()
{
    bool guessed[26] = {false};
    char guess;
    char solution[11];  //word that the user inputs in
    int wrong_count(0);

    while (false)
    {
        cout << "Enter a word no more than ten letters: ";
        cin.getline (solution, 10);
        if (strlen(solution) <= 10)
            return true;
        else
            return false;
    }

    lowercase(solution);
    cout << "The word is: " << solution << endl;

    while (wrong_count < 6)
    {
        cout << "Please enter your guess: " << endl;
        cin >> guess;
        cout << "You've entered the letter " << char(tolower(guess)) << endl;

        if (bool (guessed [guess - 'a'] == true))
        {
            cout << "You've guessed " << char(tolower(guess)) << " already." << endl;
            //for some reason, the program ends when I enter two guesses. should I use the continue function, so it doesn't stop the program.
        }
        else
            guessed[guess - 'a'] = true;

        if (in (guess, solution))
            cout << "Correct" << endl;
        else
            wrong_count++;
    }

    if (wrong_count ==6)
        cout << "You lose" << endl;
    else 
    {
        cout << "You win" << endl;
        cout << "The word was " << solution << endl;
    }
    return 0;
}
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.