How many variables does this program have
Oct 25, 2013 at 6:52pm UTC
im not to sure if identified them correctly
but Im counting 15
am I wrong?
Im just a beginner and this is a book problem
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>
using namespace std;
string THE_WORD; // word to guess
int wrong;
string soFar;
string used;
bool match(char letter, string word);
char askGuess(string usedLettersStr); //tells the compiler of method askGuess
bool playAgain();
int main()
{
srand(time(0));
vector<string> words; // collection of possible words to guess
words.push_back("GUESS" );
words.push_back("HANGMAN" );
words.push_back("DIFFICULT" );
words.push_back("BOOK" );
words.push_back("DIGITAL" );
words.push_back("PROGRAM" );
cout << "Welcome to Hangman. Good luck!\n" ;
// loop starts here
bool done = false ;
do
{
const int MAX_WRONG = 8; // maximum number of incorrect guesses allowed
random_shuffle(words.begin(), words.end());
THE_WORD = words[0]; // word to guess
soFar = string(THE_WORD.size(), '-' ); // word guessed so far
used = "" ; // letters already guessed
// loop for current word
while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
{
cout << "\n\nYou have " << (MAX_WRONG - wrong) << " incorrect guesses left.\n" ;
cout << "\nYou've used the following letters:\n" << used << endl;
cout << "\nSo far, the word is:\n" << soFar << endl;
used += askGuess(used);
} // end of while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
// shut down
if (wrong == MAX_WRONG)
{
cout << "\nYou've been hanged!" ;
}
cout << "\nThe word was " << THE_WORD << endl;
} while (playAgain());
return 0;
}
inline bool match(char letter, string word)
{
return ( word.find(letter) != string::npos );
}
char askGuess(string usedLettersStr)
{
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)
while (match(guess, used))
{
cout << "\nYou've already guessed " << guess << endl;
cout << "Enter your guess: " ;
cin >> guess;
guess = toupper(guess);
}
// if (THE_WORD.find(guess) != string::npos)
if (match(guess, THE_WORD))
{
cout << "That's right! " << 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" ;
++wrong;
}
}
bool playAgain() // function to play again while clearing system
{
char again;
cout << "\n\nWould you like to play again? <y/n>: " ;
cin >> again;
cin.clear(); //clear and ignore cin
cin.ignore();
again = toupper(again);
system("cls" );
return (again == 'Y' );
}
Oct 25, 2013 at 7:53pm UTC
There are 12.
The "variable" names in a function declaration are just placeholders; only the type is needed. So, don't count
letter ,
word or
usedLettersStr on lines 15 and 16. Those lines could be rewritten like so:
1 2
bool match(char , string);
char askGuess(string); //tells the compiler of method askGuess
Oct 25, 2013 at 8:09pm UTC
Oh okay thank you so much!
Topic archived. No new replies allowed.