Vote count:
-1
Hello I am posting this because I have a bunch of other things to move on to other things and this is frying my brain. It's heavily commented with the major issues commented in CAPS. Clearly, flow control is not my forte. Thanks if anyone can help.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <string>
#include <vector>
using namespace std;
//Container to hold the words we read from the file
vector<string> words;
char keepon = 'Y';
//function declarations
bool readFile();
int Play_Game(const string&);
int main()
{
int which_word;
cout << "Welcome to Hangman" << endl;
if (!readFile())
return 0;//Could not open file
{ //ISSUE - THIS IS NOT WORKING; NOT PICKING A RANDOM WORD,
//ITS ALWAYS THE SECOND LETTER IN THE LIST
//Pick a word at random from the vector
which_word = rand() % words.size();
//Pass the random word chosen
Play_Game(words[which_word]);
}
cout << "Thanks for playing." << endl;
return 0;
}
bool readFile()
{
int count = 0;
string word;
ifstream Wordslist;
Wordslist.open("inputlab9.txt");
if (!Wordslist.is_open())
return false;
while (Wordslist >> word)
{ //successfully read a word
//Add it to the vector
words.push_back(word);
}
Wordslist.close();
return true;
}
int Play_Game(const string& word)
//can access length of word with word.length() and character with word[i]
{
char response;//user's letter guess
int misses = 0;//keeps track of misses
size_t displayed = 0;//holds value of letters already displayed
//size_t to represent sizes and counts
do
{
cout << "Do you want to play (Yes or No)? " << endl;
//ISSUE - AFTER ONE GAME THIS KEEPS REPEATING THE GUESSED WORD FROM THE FIRST GAME
cin >> keepon;
keepon = toupper(keepon);
string solution = word;
for (size_t i = 0; i < solution.length(); i++)
{
solution[i] = '*';//establish stars to "cover" hidden letters
}
while (displayed < word.length())//While displayed letters are less than word length
{
cout << "Misses so far :" << misses << endl;
cout << "Enter a letter in the word" << endl;
cout << solution << endl;//display all the stars "covering" hidden word
cin >> response;
response = tolower(response);
bool rightguess = false;//flag assuming guess is false initially
bool alternative = false;//had to add second boolean
for (size_t i = 0; i < word.length(); i++)//loop through the word
if (response == word[i])//if guess is letter in the word
if (solution[i] == word[i])//if letter is already displayed
{
cout << response << "is already in the word."
<< endl;
alternative = true;//had to add second boolean
break;//sends down to next if statement
}
else
{
solution[i] = word[i];//changes star to letter in
hidden word
displayed++;//displays hidden letter
if (displayed != 0)
{
cout << "The letter appears " << displayed
<< " times." << endl;
//ISSUE-ITS JUST COUTING AMOUNT OF
OCCURENCES A LETTER HAS BEEN
//DISPLAYED; NEEDS TO COUNT INSTANCES
}
rightguess = true;//once right guess change from
false to true
}
if (alternative)
continue;//for returning to for loop otherwise will count as a miss
if (!rightguess)//if guess is wrong
{
misses++;//increase miss count
cout << response << "is not in the word." << endl;
if (misses >= 5);//user can get up to 5 misses
{
cout << "You have reached the misses limit. Goodbye." <<
endl;
return 0;//game ends
}
}
}//END WHILE
cout << "Yes, the word was " << word << endl;//when all hidden letters are displayed
cout << "You missed " << misses << "to guess the word " << word << endl;
} while (keepon == 'Y');//END DO WHILE
return 0;
}
asked 1 min ago
A few issues in a C++ game
Aucun commentaire:
Enregistrer un commentaire