Dec 5, 2017 at 2:10pm UTC
I can't seem to get this code to run. Error is on line 64 and says unknown was not declared in the scope. How do I fix that.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
const int MAX_TRIES=6;
int letterFill (char,string,string&);
int main()
{
string name;
char letter;
int num_of_wrong_guesses=0;
string word;
string words[]=
{
"falcons",
"saints",
"buccaneers",
"panthers",
"packers",
"bears",
"lions",
"vikings",
"49ers",
"rams",
"seahawks",
"cardinals",
"cowboys",
"giants",
"redskins",
"eagles",
"patriots",
"bills",
"dolphins",
"jets",
"titans",
"colts",
"jaguars",
"texans",
"broncos",
"cheifs",
"raiders",
"chargers",
"steelers",
"bengles",
"browns",
"ravens",
};
srand(time(NULL));
int n=rand()%32;
word=words[n];
cout << "\n\nWelcome to hangman...Guess a NFL team name!";
cout << "\n\nEach letter is represented by a star.";
cout << "\n\nYou only type one letter in one try!";
cout << "\n\nYou have"<<MAX_TRIES<<"errors to try and guess the team.";
cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
while(num_of_wrong_guesses < MAX_TRIES)
{
cout << "\n\n" << unknown;
cout << "\n\nGuess a letter:";
cin >> letter;
if(letterFill(letter,word,unknown)==0)
{
cout << endl <<"Whoops! That letter isnt't in there" << endl;
num_of_wrong_guesses++;
}
else
{
cout << "You found a letter!!!" << endl;
}
cout << "You have"<<MAX_TRIES-num_of_wrong_guesses;
cout << "errors left!" << endl;
if(word==unknown)
{
cout << word << endl;
cout << "Yeah! You got it!!!";
break;
}
}
if(num_of_wrong_guesses==MAX_TRIES)
{
cout << "\nSorry, you lose! Better luck next time." << endl;
cout << "The word was"<<word<<endl;
}
cin.ignore();
cin.get();
return 0;
}
Dec 5, 2017 at 2:14pm UTC
You're trying to use a variable, unknown , that doesn't exist. What's it meant to be? Is it a number? Is it a string? Is it a float? What's it meant to be?