#include "stdafx.h" #include <string> #include <iostream> #include <ctime> #include <cctype> #include <vector> #include <algorithm> using namespace std; char getGuess(string &used); //Prototype Function Declaration char EntryIsCorrect(); //Prototype Function Declaration int main() { // set-up const int MAX_Incorrect = 5; // maximum number of incorrect guesses allowed vector<string> words; // collection of possible words to guess display_hidden_word words.push_back("CALIFORNIA!"); srand(static_cast<unsigned int>(time(0))); random_shuffle(words.begin(), words.end()); const string SECRET_WORD = words[0]; // word to guess int wrong = 0; // number of incorrect guesses string soFar(SECRET_WORD.size(), '-'); // word guessed so far string used = ""; // letters already guessed cout << "Welcome to the game of Hangman!\n"; // main loop while ((wrong < MAX_Incorrect) && (soFar != SECRET_WORD)) { cout << "\n\nYou have " << (MAX_Incorrect - wrong); cout << " incorrect guesses left.\n"; cout << "\nYou have used the following letters:\n" << used << endl; cout << "\nCurrently, the word is:\n" << soFar << endl; char guess = getGuess(used); if (SECRET_WORD.find(guess) != string::npos) { cout << "That is correct! " << guess << " is in the word.\n"; // update soFar to include newly guessed letter for (unsigned int i = 0; i < SECRET_WORD.length(); ++i) { if (SECRET_WORD[i] == guess) { soFar[i] = guess; } } } else { cout << "Incorrect! " << guess << " is not in the word.\n"; ++wrong; } } // shut down if (wrong == MAX_Incorrect) cout << "\nYou have lost!"; else cout << "\nYou guessed the word!"; cout << "\nThe word was " << SECRET_WORD << endl; system("pause"); return 0; } void display_hidden_word( const std::string& word, const std::vector<char>& already_found ) { for(size_t i(0); i < word.size(); ++i) cout << ( !isalpha(word[i]) || (std::find(already_found.begin(), already_found.end(), word[i]) != already_found.end()) ? word[i] : '-' ); } //----getGuess Function Return starts here--- char getGuess(string & used) { char guess; cout << "\n\nEnter your guess: "; cin >> guess; guess = toupper(guess); //make uppercase since secret word in uppercase while (used.find(guess) != string::npos) //if guess IS in the string, ask for guess again in while loop { cout << "\nYou have already guessed " << guess << endl; cout << "Please enter another guess: "; cin >> guess; guess = toupper(guess); } used += guess; return guess; } |
|
|
|
|
|
|
display_hidden_word(word,already_found)
is too early on and won't display anything because word is empty. You need to move that function call down to after you have initially constructed the soFar variable and call it with
|
|
|
|
|
|
|
|
Anyway, I used an inline lambda in that example but the for_each style has the benefit of that you can define different lambdas and make cleaner code, e.g.: |
Everything is in one line, and there are much fewer keywords and syntax characters (auto, :, &, {}, ... etc ) |
std::for_each(SECRET_WORD.begin(), soFar.end(), subCorrectGuess);
|
|
and there are much fewer keywords and syntax characters |
display_hidden_word(soFar, already_used);
,display_hidden_word(soFar, already_used);
I assume you meant display_hidden_word(soFar, already_found);
? since I don't have a variable called already_used.void display_hidden_word
which is on line 147 according to the codepad pasted below.Also, I ran into another problem, when I include lowercase letters in the secret word, such as changing CALIFORNIA! to California!, when you try to guess by typing a or capital A, it says the letter a or A is incorrect and is not in the word when it actually is in the secret word California! |
|
|
|
|
|
|
|
|
|
|
|
|
|
|