I currently have a problem where the program when it is running will also choose one word and one letter in the word file. For example, in the word file the program only chooses the 2nd word "Dependent" The only letter that is considered correct is "d"

My code is :

#include <iostream>  
#include <fstream>
#include <string>

using namespace std;

string LoadFile();
void RunProgram(string word);
void displayHangMan();

string LoadFile() {
    int counter = 0;
    ifstream textfile;
    string s;
    string words[20];

    textfile.open("wordfile.txt");

    for (counter = 0; !textfile.eof(); counter++)
    {
        getline(textfile, s);
        words[counter] = s;
    }

    int r = rand() % counter;

    textfile.close();

    return words[r];
}

void RunProgram(string word) {
    int r;
    int WordSize;
    int guessesLost = 0;
    int totalGuesses = 8;
    int lettersRemaining;
    string letter;
    char guess;
    bool guessChecker;

    WordSize = word.size();
    lettersRemaining = WordSize;

    for (r = 0; r < word.size(); r++) {
        letter += '*';
    }

    while (totalGuesses > 0 && guessesLost < 8) {
        cout << "Guess a letter: ";
        cin >> guess;

        for (r = 0; r < word.size(); r++) {
            if ((char)tolower(guess) == (char)tolower(word[r])) {
                if (guess == word[r]) {
                    cout << "Correct" << endl;
                    break;
                }
            }
            else {
                cout << "One Guess Lost, You have " << totalGuesses-- << " left" << endl;
                guessesLost++;
                break;

            }
        }

        for (r = 0; r < word.size(); r++) {
            if (guess == word[r]) {
                cout << guess << endl;
                lettersRemaining--;
                break;
            }
            else {
                cout << "*";
            }
        }
    }
    if (guessesLost != totalGuesses) {
        cout << "Sorry, User ran out of chances " << endl;
        cout << "The word was : " << word << endl;
        system("PAUSE");
    }
    else if (lettersRemaining == 0) {
        cout << ("User wins! The word was: ") << word << endl;
        system("PAUSE");

    }
}

void main(){

    cout << "-------Hangman Game -------" << endl;

    string word = LoadFile();
    RunProgram(word);

}

So, what is your problem?

commented: So far, it looks like they are new to asking the questions. +10

The question IS a bit vague, OP. I figured out what you meant, but only after going through the code, seeing what was wrong, then going back to your words. Perhaps a better phrasing might be something like...

I wrote a hangman program. There is a text file containing at most 20 words. The game is supposed to pick a random word from that file and the player is supposed to guess the letters in that word. If all the letters are guessed with fewer than eight incorrect guesses, the player wins the game. If not, the player loses. Problem 1 is that the game always picks the same word from the file instead of randomly picking a word (it always picks the word "Dependent" instead of any other word in the file). Problem 2 is that when guessing the letters in "Dependent", if the user picks any letter other than the first letter 'd', my program incorrectly counts that as an incorrect guess.

For problem 1, try seeding the random number generator. Stick this line at the top of main...

srand(time(NULL));

You should #include the ctime and cstdlib libraries for this line. See if your word choice gets more random.

For your other problem, you have an if statement inside of another if statement on lines 54 and 55. They both seem to be checking the same thing, so why do you have two of them? Try deleting lines 55 and 58 and see if the program does not perform exactly the same. If it does, you don't need lines 55 and 58. If it DOESN'T perform exactly the same, then that's a bug in itself. If I guess 'E' or 'e', that should be a good guess and I need only ONE if statement to test that guess, not two.

So delete lines 55 and 58. Now look at your code. I see a "break" statement in the "if" part of the code and a "break" statement in the "else" part of the code, so no matter what, I'm hitting a "break" statement every time I go through the for loop from lines 53 to 66. Read this part again...

I'm hitting a "break" statement every time I go through the for loop.

Now answer this question: How many times am I going through the for loop?

With that answer in mind, read problem 2 again.

Problem 2 is that when guessing the letters in "Dependent", if the user picks any letter other than the first letter 'd', my program incorrectly counts that as an incorrect guess.

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.