Hmmm...I completed my console hangman game last night and it was working fine. I got up this morning and fiddled with it a bit and must've done something I was unaware of and slightly broke the program's ability to recognize either the answer guessed was correct or not. It prints out whether you've guessed it or not but it continues to loop through with the number of guesses left restarting, but at -1, -2, etc.

It should tell the user whether they've guessed it or not and go no further but to "Hit Enter to quit" type thing. I will post my code but the lines in question are 98-108.

Can you figure out what's going on or point me in a direction to go?

#include <iostream>
#include <string>
#include<conio.h>  //allows me to use getche() to have a silent delay instead of seeing, "Press ENTER to continue...."
#include <ctime>  //allows me to make use of clock() so I can have a totally useless "LOADING GAME...." effect 
#include <map>   //allows me access to member type const_iterator (constant bidirectional) begin(), end() & size().


using namespace std;

void wait (int seconds)
{
clock_t endwait;
endwait = clock() + seconds * CLOCKS_PER_SEC;
while (clock() < endwait) {}
}

string strtolower(string str);         //Converts all letters to lowercase if user guesses in capitals
string LettersUsed(map <string, int> letters); //Used so I can count the occurences of letters
string displayWord(string hiddenWord, string secretWord, string letter);

int main()
{

  //VARIABLE DECLARATIONS
    int maxGuesses = 7;
    string secretWord;
	secretWord = "mountaintop";
    secretWord = strtolower(secretWord);                 // Converts the secret word to lowercase from strtolower()
    string hiddenWord = string(secretWord.size(), '-');  // Hides each secretWord letter with '-'

    map <string, int> lettersUsed; // Keeps track of individual letters used.
    int guesses = 0; 
    string letter; 

  //INTRODUCTION TO GAME
    cout << " " <<endl;
	cout <<"\t\t\t  +++++++++++++++++++++++++++"<<endl;
	cout << " "<<endl;
    cout <<"\t\t\t\t    HANGMAN!" <<endl;
	cout << " " <<endl;
	cout <<"\t\t\t  +++++++++++++++++++++++++++"<<endl;
	cout<<"\n\n";

        cout<<"\t\t\t\t   +----+     "<<endl;
		cout<<"\t\t\t\t   |    |     "<<endl;
		cout<<"\t\t\t\t   |    O     "<<endl;
		cout<<"\t\t\t\t   |   /|\\   "<<endl;
		cout<<"\t\t\t\t   |   / \\   "<<endl;
		cout<<"\t\t\t\t   |          "<<endl;
		cout<<"\t\t\t\t  ============"<<endl;

	cout <<"\n\n\n The game will begin in 5 seconds" <<endl;
	cout <<"\n Press ENTER to begin loading:\n" <<endl;
	getche();

	cout << "\n\n\t\t\t\tLOADING GAME";
		for (int countdown = 0; countdown < 5; countdown++)
		{
			cout <<".";
			wait(1);
		}

    system("cls");   //Clears screen after totally useless 'Loading Game' is "done" loading.

	cout <<"\n\n DIRECTIONS:" <<endl;
	cout <<"\n 1. Try to guess the computer's secret word. " <<endl;;
	cout <<" 2. Guess your letter one at a time." <<endl;
	cout <<" 3. HANGMAN contains 7 letters; you will have a maximum of 7 guesses." <<endl;
	cout <<" 4. If you exceed the maximum guesses, you will be considered hung." <<endl;

    cout <<"\n\n\nThe secret word has " << secretWord.length() << " characters." <<endl
    << "You have " << maxGuesses << " incorrect guesses allowed.\n" <<endl;

    while (1) {
        cout <<"\n" <<hiddenWord <<endl;

        cout << "\nGuesses remaining: " << maxGuesses - guesses <<endl;   //Forumula to calculate remaining guesses
        cout << "\nLetters used: " << LettersUsed(lettersUsed) <<endl;    
        cout << "Enter a letter: ";
        cin  >> letter;

        letter = strtolower(letter);

        if(lettersUsed[letter]) {   //If a letter is entered twice
            cout << "You have ALREADY USED that letter!" <<endl;
            continue;
        }

        lettersUsed[letter]++; // When a letter is entered for first time, it is marked as used.
        if(secretWord.find(letter) == string::npos)   //npos used if guessed letter = unsuccessful find in the secret word
        {
            guesses++;   //Increase guess count
        }
        
        hiddenWord = displayWord(hiddenWord, secretWord, letter);   //Regenerates the hidden word, revealing each occurence of that letter, if it exists.

        
        if(secretWord == hiddenWord) {   //Was the secret word guessed?
			cout << "\n\n\tCONGRATULATIONS! YOU GUESSED THE SECRET WORD: " <<secretWord <<endl;
			
		}

        if(guesses == maxGuesses) {  // Checks to see if the max # of guesses has been reached
            cout << "\n\nSORRY, BUT YOU JUST HUNG YOURSELF!" <<endl;
            cout<<"\n\nThe secret word was: " << secretWord <<endl;
		}
    }
    return 0;
}
  
               //FUNCTIONS
// #############################################
string strtolower(string str) {
     string retval = str;         

    for(size_t i = 0; i < str.length(); i++) {
        retval[i] = tolower(str[i]);
    }
    return retval;
}

// ##############################################
string LettersUsed(map <string, int> letters) {
    string retval;

    for(map < string, int>::const_iterator it = letters.begin(); it != letters.end(); it++) {  /*Will return an iterator to the matching letter if it is found
	                                                                                             or to the end of the map if the letter is not found*/
       retval += it->first;  
    }
    return retval;
}

// ##############################################
string displayWord(string hiddenWord, string secretWord, string letter) {
    for(unsigned int i = 0; i < hiddenWord.size(); i++) {
        if(hiddenWord[i] == '-' && secretWord[i] == letter[0]) {
            hiddenWord[i] = letter[0];  // Show revealed letter(s).
        }
    }
    return hiddenWord;
}

what is string::npos for? help says it's a static const basic_sting , generic bad/missing length/position

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.