I am trying to make a hangman game where the user can either enter a single letter to guess or a whole word to guess but dont know how to allow the user to enter one or the other. Am i supposed two have two different functions, one for the single letter input and one for the whole word input. Or can you put this into one overall guess function.
Any help would be awesome!!
#include<iostream>
#include<string>
#include<fstream>
#include <time.h>
using namespace std;
///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];
}
}
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);
I think you may need two different functions. I did notice though that main doesn't return anything. Main always needs to return an integer. When you write out main it should look like this
any ideas for how i would let the program know if the user entered a char letter or a string word?
Right now when i enter a word, because letter is a char variable it takes the word as all separate letters but i don't know how to let the user enter a single letter or a whole word without letting them enter both.
Not sure I understand the question, but if you just read in a string, you can check the size to see if its a single letter or not.
1 2 3 4 5 6 7 8 9 10 11 12
char letter(0);
string word("");
cin >> word;
if (word.size() == 1)
{
// user entered a letter;
letter = *(word.begin());
}
else
{
// user entered a word
}
Apologies if I come off as patronising, that is no my intention
Hmmm....I know global variables have their own usage and stuff but I discourage using them especially if they will be modified or something later in your codes. :)
any ideas for how i would let the program know if the user entered a char letter or a string word?
You can try looking at the size of the string. Because basically, a string is a group of chars. :) You can always use string.length() to return an unsigned int type of the number of chars. That means, if it is == to 1, then it is a char, else... :)