Hangman Game Problem

Hello,

I am making a hangman game where the user can enter a single letter guess or whole word guess.

But when the user does a whole word guess and gets it right it is supposed to flag a win.

Also i am not sure how to terminate the while loop in the main function when the user gets the word correct.

#include<iostream>
#include<string>
#include<fstream>
#include <time.h>
using namespace std;


string dictionaryArray[416];
char ast[10] = { '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', };
string guess;
char singleLetter;
int noGuesses = 0;





///This function is going to be used to get the player to enter their name
string playername(string prompt){
string a;/// creates a variable with identifier a
cout << endl << prompt;
getline(cin, a);/// prompts the user to enter a string for the variable a
return a;///return whatever the user entered to the function
}

///This function will load a list of words from a plain text file
void loadDictionary(){
string words; /// Declares a variable with identifier word
ifstream dictFile("H:\\Visual Studio 2013\\Projects\\C++ Game Project\\Dictionary.txt", ios::in); /// Finds the file using the path provided
if (!dictFile.fail()) { ///Checks to see if the file exists
for (int i = 0; i < 416; i++){///Loops through every element in the dictionary
getline(dictFile, words);
dictionaryArray[i] = words;/// Put every word into the array

}
dictFile.close(); ///Closes the file
}
else
{
cout << "The file does not exist"; /// Tells the user if the file exists or not
}

}


///this function will select a random word from the list
string randomword() {
int randnum;
string randWord;
srand((unsigned)time(NULL)); ///seed random number generator
randnum = rand() % 416; /// generate a random number between 0 and 415
randWord = dictionaryArray[randnum]; /// takes the word from the dictionary
return randWord;
}


void printAsterisks(int b){
for (int i = 0; i < b; i++){
cout << ast[i];
}
}

int letterGuess(string selectedword){

int matches = 0;
char singleLetter;
string wholeWord;
cout << " >> Your Guess: ";


cin >> guess;
noGuesses++;

if (guess.size() == 1){

char singleLetter = guess[0];

for (int i = 0; i < selectedword.length(); i++){
if (singleLetter == (selectedword.at(i)))
{
ast[i] = singleLetter;
matches++;
}
else if (guess.size() > 1){
wholeWord = guess;

for (int i = 0; i < selectedword.length(); i++){
if (wholeWord.at(i) == (selectedword.at(i)))
ast[i] = wholeWord[i];
matches++;
}



}


}

}
return matches;
}







int score(int totalmatches, int maxTries, string selectedword){

int score = 0;
for (int i = 0; i < maxTries; i++){
if (ast[i] == selectedword.at(i)){
score = maxTries + 1 - noGuesses;
}
}
return score;
}



int main(){
string name; /// Creates a variable called name
string selectedword; ///Creates a variable with identifier selectedword
int maxTries;
int totalmatches = 0;
int totalScore;

name = playername("Please enter your name: "); /// Asks the user to enter their name
loadDictionary();/// Calls the loadDictionary function
selectedword = randomword();
maxTries = selectedword.length();
cout << selectedword;
do{
printAsterisks(selectedword.length());
totalmatches = letterGuess(selectedword);
totalScore = score(totalmatches, maxTries,selectedword);
} while (noGuesses < maxTries);
cout << endl << name << " scores " << totalScore;



cout << endl << "The end!";

return 0;
}


You can try something like this

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
int main(){
string name; /// Creates a variable called name
string selectedword; ///Creates a variable with identifier selectedword
int maxTries;
int totalmatches = 0;
int totalScore;

name = playername("Please enter your name: "); /// Asks the user to enter their name
loadDictionary();/// Calls the loadDictionary function
selectedword = randomword();
maxTries = selectedword.length();
cout << selectedword;
do{
printAsterisks(selectedword.length());
totalmatches = letterGuess(selectedword);	
totalScore = score(totalmatches, maxTries,selectedword); 
} while (noGuesses < maxTries || selectedword != randWord); //Will keep doing the loop until the selected word equals the random word. 
                                                           //or until the player uses up all of their guesses.
cout << endl << name << " scores " << totalScore;



cout << endl << "The end!";

return 0;
} 
} 
Topic archived. No new replies allowed.