I am trying to do a hangman project, but my code isn't working. Whenever I put in the proper letter, the code tells me it is wrong (even though it is right). Not really sure why - the code worked at some point but I changed some things and now I don't know why it doesn't work. So it is probably a simple fix, but I am just not seeing it.
Any help would be very appreciated!
#include <iostream>
using namespace std;
int letterFill (char, string, string&);
int main()
{
string name;
int maxAttempts = 5;
int wrongGuesses;
char letter;
srand(time(0));
const string wordList[15] = { "hanukkah", "sparklers", "mistletoe", "menorah", "presents", "reindeer",
"kwanzaa", "snowman", "eggnog", "celebration", "yuletide", "resolution", "nutcracker", "ornaments", "gingerbread" };
string correctWord = wordList[rand() % 15];
string unknown(correctWord.length(),'*');
cout << correctWord << endl;
cout << "Welcome to a fun game of winter holiday hangman! What is your name? " << endl;
cin >> name;
cout << name <<", there are some simple things you should know about this game before you start playing!" << endl;
cout << "You will be trying to guess a randomly selected word by typing in ONE letter at a time " << endl;
cout << "You will have " << maxAttempts << " tries before losing the game " << endl;
cout << "And remember, all of the words are winter holiday related. Good luck " << name <<"!" << endl;
cout << "*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*" <<endl;
while (wrongGuesses == 0)
{
cout << "Guess a letter" << cout;
cin >> letter;
if (letterFill(letter, correctWord, unknown)==0)
{
cout << endl << "That letter is not in this word! Try again " << endl;
wrongGuesses = wrongGuesses + 1;
}
else
{
cout << endl << "You found a letter! Keep up the good work! " << endl;
}
if (correctWord==unknown)
{
cout << correctWord << endl;
cout << "Congratulations! You guessed the correct word!" << endl;
}
}
while (wrongGuesses == 1)
{
cout << "You have 4 guesses left " << endl;
cout << "Guess a letter " << cout;
cin >> letter;
if (letterFill(letter, correctWord, unknown)==0)
{
cout << endl << "That letter is not in this word! Try again " << endl;
wrongGuesses = wrongGuesses + 1;
}
else
{
cout << endl << "You found a letter! Keep up the good work! " << endl;
}
if (correctWord==unknown)
{
cout << correctWord << endl;
cout << "Congratulations! You guessed the correct word!" << endl;
}
}
while (wrongGuesses == 2)
{
cout << "You have 3 guesses left " << endl;
cout << "Guess a letter " << cout;
cin >> letter;
if (letterFill(letter, correctWord, unknown)==0)
{
cout << endl << "That letter is not in this word! Try again " << endl;
wrongGuesses = wrongGuesses + 1;
}
else
{
cout << endl << "You found a letter! Keep up the good work! " << endl;
}
if (correctWord==unknown)
{
cout << correctWord << endl;
cout << "Congratulations! You guessed the correct word!" << endl;
}
}
while (wrongGuesses == 3)
{
cout << "You have 2 guesses left " << endl;
cout << "Guess a letter " << cout;
cin >> letter;
if (letterFill(letter, correctWord, unknown)==0)
{
cout << endl << "That letter is not in this word! Try again " << endl;
wrongGuesses = wrongGuesses + 1;
}
else
{
cout << endl << "You found a letter! Keep up the good work! " << endl;
}
if (correctWord==unknown)
{
cout << correctWord << endl;
cout << "Congratulations! You guessed the correct word!" << endl;
}
}
while (wrongGuesses == 4)
{
cout << "You have 1 guess left " << endl;
cout << "Guess a letter " << cout;
cin >> letter;
if (letterFill(letter, correctWord, unknown)==0)
{
cout << endl << "That letter is not in this word! Try again " << endl;
wrongGuesses = wrongGuesses + 1;
}
else
{
cout << endl << "You found a letter! Keep up the good work! " << endl;
}
if (correctWord==unknown)
{
cout << correctWord << endl;
cout << "Congratulations! You guessed the correct word!" << endl;
}
}
while (wrongGuesses == 5)
{
cout << "Sorry " << name << " you have made 5 wrong guesses!" << endl;
cout << "Game over. Click any key to exit. Play again soon :) " << endl;
if (letterFill(letter, correctWord, unknown)==0)
{
cout << endl << "That letter is not in this word! Try again " << endl;
wrongGuesses = wrongGuesses + 1;
}
else
{
cout << endl << "You found a letter! Keep up the good work! " << endl;
}
if (correctWord==unknown)
{
cout << correctWord << endl;
cout << "Congratulations! You guessed the correct word!" << endl;
}
}
system("pause");
return 0;
}
int letterFill (char guessLetter, string mysteryWord, string& guessWord)
{
int x;
int matches=0;
int lengthWord=mysteryWord.length();
for (x = 0; x< lengthWord; x++)
{
if (guessLetter == mysteryWord[x])
return 0;
if (guessLetter == mysteryWord[x])
{
guessWord[x] = guessLetter;
matches++;
}
}
return matches;
}
You aren't updating the string guessWord in your int letterFill() function. As soon as you see a letter that matches you return without entering that second if statement.
I assume what you want is only to return after fully updating the guessWord, based on that what you want to do is iterate through the string, updating guessWord as you find matches and after your loop do a check
if(matches == 0) return 0;
else return matches;
Related
My C++ program wants the user to choose a test option. Option A wants the user to login their id, then they will proceed to take the test. Option B gives the user the results of the taken test. Finally, Option C will make the user quit.
I am trying to get the function getStatistics() to display whether the question that what the user answered is right or wrong, display the user answers and the correct answer.
`
#include <iostream>
#include <string>
# include <cctype>
using namespace std;
const int SIZE = 8;
int totalRightScore{};
int result = totalRightScore;
char ch;
char answer[SIZE] = { 'a', 'b', 'c', 'd' , 'A' , 'B' , 'C' , 'D' };
void test() {
char choice;
cout << "1. What is the capital of USA?" << endl;
cout << "(a)Georgia" << endl;
cout << "(b)Washington, Dc" << endl;
cout << "(c)Berlin" << endl;
cout << "(d)New York City" << endl;
cin >> ch;
if (ch == answer[1] || ch == answer[5]) {
totalRightScore = totalRightScore + 1;
}
cout << "2. How many stars has the USA flag have" << endl;
cout << "(a)50" << endl;
cout << "(b)60" << endl;
cout << "(c)35" << endl;
cout << "(d)13" << endl;
cin >> ch;
if (ch == answer[0] || ch == answer[4]) {
totalRightScore = totalRightScore + 1;
}
cout << "3. What is the name of your SQL instructor?" << endl;
cout << "(a)Alan Anderson" << endl;
cout << "(b)Dr House " << endl;
cout << "(c)Bill Nye" << endl;
cout << "(d)Dr Gill" << endl;
cin >> ch;
if (ch == answer[0] || ch == answer[4]) {
totalRightScore = totalRightScore + 1;
}
}
void loginid() {
string login = "test123";
cout << "enter your login id" << endl; ;
cin >> login;
if (login == "test123") {
test();
}
else
{
cout << "Sorry wrong user ID";
}
}
void getStatistics() {
cout << " Question 1 ";
if (ch == answer[1] || ch == answer[5]) {
cout << "correct" << endl;
cout << "your answer is " << endl;
cout << ch << endl;
cout << "correct answer is " << endl;
cout << "(b) Washington, Dc" << endl;
}
else
{
cout << "incorrect" << endl;
cout << "your answer is " << endl;
cout << ch << endl;
cout << "correct answer is " << endl;
cout << "(b)Washington, Dc" << endl;
}
cout << " Question 2 ";
if (ch == answer[0] || ch == answer[4]) {
cout << "correct" << endl;
cout << "your answer is " << endl;
cout << ch << endl;
cout << "correct answer is " << endl;
cout << "(a) 50" << endl;
}
else
{
cout << "incorrect" << endl;
cout << "your answer is " << endl;
cout << ch << endl;
cout << "correct answer is " << endl;
cout << "(a) 50" << endl;
}
cout << " Question 3 ";
if (ch == answer[0] || ch == answer[4]) {
cout << "correct" << endl;
cout << "your answer is " << endl;
cout << ch << endl;
cout << "correct answer is " << endl;
cout << "(a)Alan Anderson" << endl;
}
else
{
cout << "incorrect" << endl;
cout << "your answer is " << endl;
cout << ch << endl;
cout << "correct answer is " << endl;
cout << "(a)Alan Anderson" << endl;
}
result = totalRightScore;
cout << "your total score is " << result << "/3 " << endl;
totalRightScore = 0;
}
void exit() {
char choice;
cout << "Press Q or q to exit program." << endl;
cin >> choice;
if (choice == 'Q'|| choice == 'q')
{
cout << "Good bye Now" << endl;
}
}
void menu() {
string keepgoing = "y";
char choice;
while (keepgoing == "y")
{
cout << " Select Your Option:\n ";
cout << "(a Login with your userID\n ";
cout << "(b Statistics\n ";
cout << "(c Quit\n ";
cin >> choice;
switch (choice)
{
case 'a':
loginid();
break;
case 'b':
getStatistics();
break;
case 'c':
exit();
break;
}
cout << "would you like to continue? type 'y' to continue " << endl;
cin >> keepgoing;
}
}
`
The problem is that getStatistics() is just getting the last input from test(), for example if you input the right answer for question 1 and input the right answer question 3, when you run the getStatistics()it only remembers the last input from test() and thus deem the right answer for question 1 incorrect. Is there anyway for the getStatistics() to remember all the user answers for all the questions from test(), instead of just getting what is last answered.Thank you.
So in my class I had to make a Numberwang simulation game. Everything works fine except for the fact that after 2 rounds the names don't correlate correctly. It supposed to say "Round 3, Player1 to play first." which it does however player2 comes up as the one to play first.
# include <iostream>
# include <ctime>
# include <cstdlib>
using namespace std;
bool numberwang(int n)
{
if(n < 100 ){
return 1;
} else {
return 0;
}
}
int main()
{
string Firstplayer, Otherplayer;
int rounds;
int counter = 1;
int number;
int win = 18;
int lose= 1;
cout << "Hello, and welcome to Numberwang, the maths quiz that simply everyone is talking about!" << endl;
cout << "What is player 1's name? ";
cin >> Firstplayer;
cout << "What is player 2's name? ";
cin >> Otherplayer;
cout << "How many rounds? ";
cin >> rounds;
cout << "Well, if you're ready, lets play Numberwang!" << endl;
while(counter <= rounds){
cout << "Round " << counter << ", " << Firstplayer << " to play first." << endl;
while(true){
cout << Firstplayer << ": ";
cin >> number;
if(numberwang(number)){
counter++;
if(counter > rounds){
cout << "That's Numberwang!" << endl;
cout << "Final scores: " << Firstplayer << " pulls ahead with " << win << ", and " << Otherplayer << " finishes with " << lose << endl;
break;
}
cout << "That's Numberwang!" << endl;
swap(Firstplayer, Otherplayer);
cout << "Round " << counter << ", " << Firstplayer << " to play first." << endl;
}
cout << Otherplayer << ": ";
cin >> number;
if(numberwang(number)){
counter++;
if(counter > rounds){
cout << "That's Numberwang!" << endl;
cout << "Final scores: " << Firstplayer << " pulls ahead with " << win << ", and " << Otherplayer << " finishes with " << lose << endl;
break;
}
cout << "That's Numberwang!" << endl;
swap(Firstplayer, Otherplayer);
cout << "Round " << counter << ", " << Firstplayer << " to play first." << endl;
}
}
}
return 0;
}
After your if-statement (line 61) you say 'Firstplayer' and then you output the 'Otherplayer'. The names do not match.
Blockquote
cout << "Round " << counter << ", " << Firstplayer << " to play first." << endl;
}
cout << Otherplayer << ": ";
cin >> number;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Hi so i have started c++ 3 days ago, read some tutorials etc. I wanted to make my own hangman game since it seemed as an easy task for a begginer but i stumbled upon an issue. Everything is working well ecept i cant seem to find a way to make 1 letter strings swap with underscores until underscores change to the missing word. So basically when you compile it you can guess the whole word only.
Here is the code:
#include <iostream>
#include <string>
using namespace std;
string player1,player2,word,underscore,guess;
int wrong=0;
int main (){
string copy = word;
cout << "----------------------Hello! Welcome to the HANGMAN game!---------- ----------" << endl;
cout << "Please type in your name, PLAYER 1" << endl;
cin >> player1;
cout << "Please type in your name, PLAYER 2" << endl;
cin >> player2;
cout << "OK " << player1 << " and " << player2 << ". Let's start with the game!" << endl;
cout << player1 << " please input the word you want " << player2 << " to guess." << endl;
cin >> word;
//space
for (int x=0; x<30; x++){
cout << endl;
}
//UNDERSCORE
while (underscore.size() != word.size()){
underscore.push_back('_');}
cout << underscore << endl;
//MAIN WHILE
while(wrong<12){
cin >> guess;
//IF GUESS ISNT LETTER
if(guess.size() > 1){
if(guess==word){
cout << "Thats the right word." << endl;
break;
}
else{
cout << underscore << endl;
cout << "Wrong word try again." << endl;
cout << "Used: " << usedguess << endl;
wrong ++;
}
}
if(underscore == word){
cout << "You win!" << endl;
break;
}
if(wrong==1){
cout << "I" << endl;
}
else if(wrong==2){
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==3){
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==4){
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==5){
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==6){
cout << "I===" << endl;
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==7){
cout << "I===" << endl;
cout << "I O" << endl;
cout << "I" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==8){
cout << "I===" << endl;
cout << "I O" << endl;
cout << "I |" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==9){
cout << "I===" << endl;
cout << "I O" << endl;
cout << "I -|" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==10){
cout << "I===" << endl;
cout << "I O" << endl;
cout << "I -|-" << endl;
cout << "I" << endl;
cout << "I" << endl;
}
else if(wrong==11){
cout << "I===" << endl;
cout << "I O" << endl;
cout << "I -|-" << endl;
cout << "I /" << endl;
cout << "I" << endl;
}
else if(wrong==12){
cout << "I===" << endl;
cout << "I O" << endl;
cout << "I -|-" << endl;
cout << "I / /"<< endl;
cout << "I YOU ARE DEAD" << endl;
cout << "Game over bro! The word was: " << word <<endl;
break;
}
}
}
To compare the strings of word and guess, you can iterate over the characters in a for-loop, and check if there is a match
string word = "hangman";
string guess = "mansomething";
string underscore = string(word.size(), '_'); // init a string with underscores equal to the length of 'word'
// iterate over the characters in word and guess
for (size_t i = 0, iend = min(word.size(), guess.size()); i < iend; i++) {
if (word[i] == guess[i])
underscore[i] = word[i]; // if the characters match at position i, update the underscore.
}
cout << underscore << endl;
Afterwards, underscore contains the following
_an____
Right now I am trying to get my scorecard to work for hangman. This is a game I can play as many times as I want and I want it to show my lowest score when I press 2 at the game menu. For some reason my logic isn't working. Can anyone help? I have attached the 3 sections of code necessary for this to work.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <fstream>
using namespace std;
int maxAttempts = 10; //max attempts possible
void poorStiff(int);
int wordFill(char, string, string&);
int main()
{
int intro;
int bodyPart = 0;
ifstream wordIn; //file stream read in
wordIn.open("WordList.txt"); //list of words used in this game
//if (!wordIn.good()) { std::cerr << "open failed.\n"; exit(1); }
char letter; //letter guessed
int numWrongGuesses = 0; //counts the number of wrong guesses
string theWord;
string words[13];
int play = 0;
bool run = true;
int makeYourSelection = 0;
int bestScore = 11;
while (run == true)
{
int attemptsGame = 0;
// put a game menu to loop that asks if you want to keep playing or exit and go to scorecard
cout << " WELCOME TO THE GAME OF HANGMAN\n\n\n\n";
cout << " Press 1 to play the game, press 2 to exit to the scorecard \n\n";
cout << " You have 10 attempts to guess the words before you get hung \n\n";
cin >> play;
while (play == 1)//loops while user has entered 1, 0 exits the game
{
for (int i = 0; i < 13; i++) //labeled 13 and not words because 13 is a constant
{
wordIn >> words[i];//replaces the file words and puts them in an array and counts them out of the file
cout << words[i] << endl;
}
srand(time(NULL));//to get a random word
int n = rand() % 12;
theWord = words[n]; //pulls word from file
wordIn.close();
string mystery(theWord.length(), '*'); //replaces word letters with asterisks
while (numWrongGuesses < maxAttempts) // while the amount of guesses is less than the max wrong guesses
{
cout << mystery << endl << endl;
cout << "You now have the length of the word represented by the *'s. \n\n";
cout << "Guess a letter \n\n";
cin >> letter;
if (wordFill(letter, theWord, mystery) == 0) //fuction call
{
bodyPart++;
poorStiff(bodyPart);
cout << "You have entered a letter that isn't in the word, guess again. \n\n";
numWrongGuesses++;
attemptsGame++;
}
else
{
for (int i = 0; i < mystery.length(); i++)
{
if (theWord[i] == letter)
{
mystery[i] = letter;
}
}
cout << "You have found one of the letters. Congratulations! \n\n";
cout << "You have: " << maxAttempts - numWrongGuesses;
cout << " guesses left \n\n" << endl;
}
if (theWord == mystery) // the word is the same as mystery
{
cout << theWord << endl;
cout << "\n\n Awesome, you guessed it. \n\n";
break;
if (attemptsGame < bestScore)
{
bestScore = attemptsGame;
}
}
}
if (numWrongGuesses == maxAttempts) //when you run out of guesses
{
cout << "Too bad, you ran out of guesses and have been hung at the gallows. \n\n";
cout << "The word you were trying to guess was " << theWord << endl;
poorStiff(bodyPart);
}
cin.ignore();
cin.get();
break;
}
while (play == 2)
{
cout << "Best Scores: \n\n";
cout << bestScore << endl;
system("pause");
return 0;
break;
}
}
system("pause");
return 0;
}
int wordFill(char guess, string theWordSecret, string&guessWord) //function for determing if you guess a letter contained
{
int i;
int hits = 0; //letter hits within the word
int many = theWordSecret.length();
for (i = 0; i < many; i++)
{
if (guess == guessWord[i])
return 0;
if (guess == theWordSecret[i])
{
guessWord[i] == guess;
hits++;
}
}
return hits;
}
void poorStiff(int bodyPart)
{
if (bodyPart == 1)
{
cout << "_______" << endl;
cout << "| }" << endl;
cout << "| O" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "_______________" << endl;
}
else if (bodyPart == 2)
{
cout << "_______" << endl;
cout << "| }" << endl;
cout << "| O" << endl;
cout << "| |" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "_______________" << endl;
}
else if (bodyPart == 3)
{
cout << "_______" << endl;
cout << "| }" << endl;
cout << "| O" << endl;
cout << "| /|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "_______________" << endl;
}
else if (bodyPart == 4)
{
cout << "_______" << endl;
cout << "| }" << endl;
cout << "| O" << endl;
cout << "| /|" << endl;
cout << "| / " << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "_______________" << endl;
}
else if (bodyPart == 5)
{
cout << "_______" << endl;
cout << "| }" << endl;
cout << "| O" << endl;
cout << "| /|" << endl;
cout << "| / |" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "_______________" << endl;
}
else if (bodyPart == 6)
{
cout << "_______" << endl;
cout << "| }" << endl;
cout << "| O" << endl;
cout << "| /|\." << endl;
cout << "| / | \." << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "_______________" << endl;
}
else if (bodyPart == 7)
{
cout << "_______" << endl;
cout << "| }" << endl;
cout << "| O" << endl;
cout << "| /|\." << endl;
cout << "| / | \." << endl;
cout << "| /" << endl;
cout << "|" << endl;
cout << "_______________" << endl;
}
else if (bodyPart == 8)
{
cout << "_______" << endl;
cout << "| }" << endl;
cout << "| O" << endl;
cout << "| /|\." << endl;
cout << "| / | \." << endl;
cout << "| / \." << endl;
cout << "|" << endl;
cout << "_______________" << endl;
}
else if (bodyPart == 9){
cout << "_______" << endl;
cout << "| }" << endl;
cout << "| O" << endl;
cout << "| /|\." << endl;
cout << "| / | \." << endl;
cout << "| / \." << endl;
cout << "| / " << endl;
cout << "_______________" << endl;
}
else if (bodyPart == 10)
{
cout << "_______" << endl;
cout << "| }" << endl;
cout << "| O" << endl;
cout << "| /|\." << endl;
cout << "| / | \." << endl;
cout << "| / \." << endl;
cout << "| / \." << endl;
cout << "_______________" << endl;
}
}
Did you try debugging? If you step through your code you will immediately find the problem, which is here:
if (theWord == mystery) // the word is the same as mystery
{
cout << theWord << endl;
cout << "\n\n Awesome, you guessed it. \n\n";
break; // problem here
if (attemptsGame < bestScore)
{
bestScore = attemptsGame;
}
// break should be here
}
You're breaking out of your loop before your checking logic. So move your break statement to after the check, because that code never gets executed.
For a start enable your compiler warnings, you'll get many from a quick look at your code.
Change this
guessWord[i] == guess;
to this
guessWord[i] = guess;
At this point
if (wordFill(letter, theWord, mystery) == 0) //fuction call
{
...
cout << "You have entered a letter that isn't in the word, guess again. \n\n";
}
else
{
...
if (theWord == mystery) // the word is the same as mystery
{
cout << theWord << endl;
cout << "\n\n Awesome, you guessed it. \n\n";
break;
...
}
}
Here, when you are entering the first if, then you will enter the second if too, which doesn't make sense. That happens because theWord and mystery are both empty strings!
Also notice that break should be after this part of code:
if (attemptsGame < bestScore)
{
bestScore = attemptsGame;
}
because as it is, this part of code will never be executed.
I suggest taking at this answer (not relevant with your logical error)
System(“pause”); - Why is it wrong?
I have started with C++ and I am in the middle of creating a hangman game, My code worked fine up until I chose to make three different levels of difficulty, My game asks the user for the difficulty level they would like to play, then instead of actually playing the game, it skips straight to the end where it says the user has guessed the word correctly. Any help appreciated!
The code is as follows :
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>
using namespace std;
void ClearScreen();
void DisplayMan0();
void DisplayMan1();
void DisplayMan2();
void DisplayMan3();
void DisplayMan4();
void DisplayMan5();
void DisplayMan6();
void DisplayMan7();
int main()
{
const int MAX_WRONG = 7; // incorrect guesses allowed
void (*pfnaDisplayMan[])() = {DisplayMan0, DisplayMan1, DisplayMan2, DisplayMan3, DisplayMan4, DisplayMan5, DisplayMan6, DisplayMan7};
vector<string> words; // Level 1
words.push_back("GREEN");
words.push_back("BANANA");
words.push_back("LAPTOP");
words.push_back("GIRAFFE");
words.push_back("PENCIL");
vector<string> wordsD1; // Level 2
wordsD1.push_back("DELICIOUS");
wordsD1.push_back("COMPUTING");
wordsD1.push_back("SOFTWARE");
wordsD1.push_back("HARDWARE");
wordsD1.push_back("TELEPHONE");
vector<string> wordsD2; // Level 3
wordsD2.push_back("BAMBOOZLED");
wordsD2.push_back("DAYDREAMER");
wordsD2.push_back("CANNIBALISM");
wordsD2.push_back("NERVOUSLY");
wordsD2.push_back("APPROACHING");
srand((unsigned int)time(0));
string THE_WORD;
string soFar;
int wordLength;
string used; // letters already guessed
cout << "\t\t HANGMAN\n";
cout << "Please enter a difficulty level [1-3] ";
int dif = 0;
while(dif < 1 || dif > 3)
{
cin >> dif;
}
cout << "You have chosen difficulty level : "<< dif << endl;
if(dif == 1)
{
random_shuffle(words.begin(), words.end());
int incorrectGuesses = 0; // number of incorrect guesses
string const THE_WORD = words[0]; // word to guess
string soFar(THE_WORD.size(), '*'); // word guessed so far
// count length of randomly chosen string and display it
wordLength = THE_WORD.length();
}
if(dif == 2)
{
random_shuffle(wordsD1.begin(), wordsD1.end());
int incorrectGuesses = 0; // number of incorrect guesses
string const THE_WORD = wordsD1[0];
string soFar(THE_WORD.size(), '*');
wordLength = THE_WORD.length();
}
if(dif == 3)
{
random_shuffle(wordsD2.begin(), wordsD2.end());
int incorrectGuesses = 0; // number of incorrect guesses
string const THE_WORD = wordsD2[0];
string soFar(THE_WORD.size(), '*');
wordLength = THE_WORD.length();
}
// main loop
while ((incorrectGuesses < MAX_WRONG) && (soFar != THE_WORD))
{
cout << "\n- There are : "<< wordLength <<" letters in the word :\t" << soFar << endl;
cout << "\n- You have guessed " <<incorrectGuesses << " times wrong out of "<< MAX_WRONG << " allowed wrong guesses.\n";
cout << "\nLetters used : " << used << endl;
cout << "=====================================================";
char guess;
cout << "\n\t\tEnter a letter : ";
cin >> guess;
guess = toupper(guess); //make uppercase since secret word in uppercase
while (used.find(guess) != string::npos)
{
cout << "\nYou've already guessed the letter " << guess << endl;
cout << "Enter another letter / word: ";
cin >> guess;
guess = toupper(guess);
}
used += guess;
if (THE_WORD.find(guess) != string::npos)
{
cout << "=====================================================\n";
cout << "- Correct, The letter " << guess << " is in the word.\n";
// update soFar to include newly guessed letter
for (int i = 0; i < THE_WORD.length(); ++i)
if (THE_WORD[i] == guess)
soFar[i] = guess;
}
else
{
cout << "Sorry, " << guess << " isn't in the word.\n";
++incorrectGuesses;
pfnaDisplayMan[incorrectGuesses]();
}
}
// shut down
if (incorrectGuesses == MAX_WRONG)
cout << "\nYou've been hanged!";
else
cout << "\nYou guessed it!";
cout << "\nThe word was " << THE_WORD << endl;
return 0;
}
void DisplayMan0()
{
using namespace std;
cout << "_______" << endl;
cout << "| |" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "________" << endl;
}
void DisplayMan1()
{
using namespace std;
cout << "_______" << endl;
cout << "| |" << endl;
cout << "| o" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "________" << endl;
}
void DisplayMan2()
{
using namespace std;
cout << "_______" << endl;
cout << "| |" << endl;
cout << "| o" << endl;
cout << "| /" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "________" << endl;
}
void DisplayMan3()
{
using namespace std;
cout << "_______" << endl;
cout << "| |" << endl;
cout << "| o" << endl;
cout << "| /X" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "________" << endl;
}
void DisplayMan4()
{
using namespace std;
cout << "_______" << endl;
cout << "| |" << endl;
cout << "| o" << endl;
cout << "| /X\\" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "________" << endl;
}
void DisplayMan5()
{
using namespace std;
cout << "_______" << endl;
cout << "| |" << endl;
cout << "| o" << endl;
cout << "| /X\\" << endl;
cout << "| /" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "________" << endl;
}
void DisplayMan6()
{
using namespace std;
cout << "_______" << endl;
cout << "| |" << endl;
cout << "| o" << endl;
cout << "| /X\\" << endl;
cout << "| / \\" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "________" << endl;
}
void DisplayMan7()
{
using namespace std;
cout << "\t\t_______" << endl;
cout << "\t\t|DONT" << endl;
cout << "\t\t|HANG" << endl;
cout << "\t\t|THE" << endl;
cout << "\t\t|MAN" << endl;
cout << "\t\t| O" << endl;
cout << "\t\t| _______ /XL" << endl;
cout << "\t\t__|_____| / \\" << endl;
}
Put incorrectGuesses out of those scopes. Because out of those scopes this variable is not declared.
if(dif == 1)
{
int incorrectGuesses = 0;
...
}
if(dif == 2)
{
int incorrectGuesses = 0;
...
}
if(dif == 3)
{
int incorrectGuesses = 0;
...
}
Should be
int incorrectGuesses = 0;
if(dif == 1)
{
...
}
if(dif == 2)
{
...
}
if(dif == 3)
{
...
}
Same issues for soFar, THE_WORD and wordLength. That part of code should be like this:
string THE_WORD;
string soFar;
int wordLength;
string used;
// cout ... cin ....
int incorrectGuesses = 0;
if(dif == 1)
{
random_shuffle(words.begin(), words.end());
THE_WORD = words[0]; // word to guess
wordLength = THE_WORD.length();
}
if(dif == 2)
{
random_shuffle(wordsD1.begin(), wordsD1.end());
THE_WORD = wordsD1[0];
wordLength = THE_WORD.length();
}
if(dif == 3)
{
random_shuffle(wordsD2.begin(), wordsD2.end());
THE_WORD = wordsD2[0];
wordLength = THE_WORD.length();
}
soFar.assign(THE_WORD.size(), '*');
M M. is correct. Your redeclaring the variables.
Just a small remark. I would use a Switch Case instead of a set of if statements. Changing:
if(dif==1){}
if(dif==2){}
if(dif==3){}
into
switch(dif){
case(1):
break;
case(2):
break;
case(3):
break;
}
Not for necessarily for readability but more to indicate that the value of dif isn't edited depending upon its value. For example:
Option 1:
dif = 1;
if(dif==1){ dif = 3; }
if(dif==2){}
if(dif==3){ dif = 7; }
Versus:
Option 2
dif = 1;
switch(dif){
case(1):
dif = 3;
break;
case(2):
break;
case(3):
dif = 7;
break;
}
Option 1 output: 7
Option 2 output: 3
You declare incorrectGuesses out of scope. It is NEVER declared or assigned a value. Declare it at the beginning of your function and assign it value in the other scopes.