C++ Hangman Game

hello,

I am trying to write code that will accept either a single letter guess for a hangman game or a whole word guess.

I think i need a function for the single letter input and another function for the whole word input.

I just don't know how to get the user to enter either a single letter or a whole word.

Here is the code so far that has the single letter input function in it.

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


string dictionaryArray[416];
char ast[10] = { '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', };
char letter;
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;

cout << " >> Your Guess: ";
cin >> letter;

noGuesses++;


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



void main(){
string name; /// Creates a variable called name
string selectedword; ///Creates a variable with identifier selectedword
int maxTries;
int totalmatches;
name = playername("Please enter your name: "); /// Asks the user to enter their name
loadDictionary();/// Calls the loadDictionary function
selectedword = randomword();
maxTries = selectedword.length();
do{
printAsterisks(selectedword.length());
totalmatches = letterGuess(selectedword);
} while (noGuesses < maxTries);




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


}

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
#include <iostream>
#include <string>

int main()
{
    std::string guess ;
    std::cout << "your guess (either a single letter or the whole word): " ;
    std::cin >> guess ;

    if( guess.size() == 1 )
    {
        // single letter
        char letter_guess = guess[0] ;

        // ...
    }

    else if( guess.size() > 1 ) // whole word
    {
        std::string whole_word_guess = guess ;

        // ...
    }

    else // guess.size() == 0, input failed
    {
        //  (you may assume that this would never happen)
    }
}
yea that looks cool! thanks man
Topic archived. No new replies allowed.