Here's what I have. It compiles and runs the game, but when I make a guess right or wrong it ends the game.
The words in the file are not separated by blank lines and idk how many are in there. (if that's significant).
The problem is in the getWord function
//Libraries
#include <iostream>
#include <vector>
#include <string>
#include <ctype.h>
#include <fstream>
using namespace std;
//Constructs
//stores a letter and whether it is hidden or revealed
struct letter{
letter(char character):character(character),hidden(true){}
char character;
bool hidden;
char display() const{
if (!hidden)
return character;
return ('_');
}
};
//Global Constants
//Function Prototypes
bool winGame(const vector<letter>&word);//returns true if letters are guessed
bool guess(vector<letter>&word);//inputs guess from user and return t/f
void dspWord(const vector<letter>&word);//displays correctly guessed letters/word
vector<letter>getWord();//Inputs secret word from user, stores in a vector
//Begin Execution
int main(){
//Declare Variables
//Prompt User
cout<<" Welcome to Hangman! "<<endl<<endl;
cout<<" You are allowed a total of 6 guesses "<<endl<<endl;
cout<<" If you think you know the whole word. Enter a '$' symbol"<<endl<<endl;
vector<letter>word;//stores secret word in a vector
unsigned bdGuess(0); //keeps track of bad guesses from user
word=getWord();//Input secret word from user
while(bdGuess<6){//User is allowed 6 guesses
dspWord(word);//display guessed and not guessed parts of word
if(!guess(word)){//increment bad guesses and output them to user
bdGuess++;
cout<<" Number of wrong guesses: "<<bdGuess<<endl;
}
if(bdGuess==1){
cout<<endl<<endl
<<" +----+ "<<endl
<<" | | "<<endl
<<" | "<<endl
<<" | "<<endl
<<" | "<<endl
<<" | "<<endl
<<" ============="<<endl<<endl;
}
if(bdGuess==2){
cout<<endl<<endl
<<" +----+ "<<endl
<<" | | "<<endl
<<" | O "<<endl
<<" | | "<<endl
<<" | "<<endl
<<" | "<<endl
<<" ============="<<endl<<endl;
}
if(bdGuess==3){
cout<<endl<<endl
<<" +----+ "<<endl
<<" | | "<<endl
<<" | O "<<endl
<<" | /| "<<endl
<<" | "<<endl
<<" | "<<endl
<<" ============="<<endl<<endl;
}
if(bdGuess==4){
cout<<endl<<endl
<<" +----+ "<<endl
<<" | | "<<endl
<<" | O "<<endl
<<" | /|\\ "<<endl
<<" | "<<endl
<<" | "<<endl
<<" ============="<<endl<<endl;
}
if(bdGuess==5){
cout<<endl<<endl
<<" +----+ "<<endl
<<" | | "<<endl
<<" | O "<<endl
<<" | /|\\ "<<endl
<<" | \\ "<<endl
<<" | "<<endl
<<" ============"<<endl<<endl;
}
if(bdGuess==6){
cout<<endl<<endl
<<" +----+ "<<endl
<<" | | "<<endl
<<" | O "<<endl
<<" | /|\\ "<<endl
<<" | / \\ "<<endl
<<" |You Died "<<endl
<<" ============"<<endl<<endl;
}
if(winGame(word)){//if all letters guessed user wins
cout<<endl<<" Congrats! You Won! The word was: "<<endl;
dspWord(word);
cout<<endl;
return 0;
}
}
cout<<" Sorry, You Lost... "<<endl;
}
vector<letter>getWord(){//Get secret word from user
//Declare Variables
//string usrWord;
//char ch;
vector<letter>vecWord;
//cout<<"Enter secret word: ";
//cin>>usrWord;
ifstream in("wordsEn.txt");
string usrWord, randWd;
in>>randWd;
while ( in>> usrWord ) {
if ( rand() < RAND_MAX / 2 )
randWd = usrWord;
cout<<usrWord;
}
//return randWd;
for(unsigned i=0;i<usrWord.length();i++)//secret word is converted and returned as a vector
vecWord.push_back(letter(toupper(usrWord[i])));
//clear the screen
system("CLS");
return vecWord;
}
//display word to user (The parts of it that are hidden and also not hidden)
void dspWord(const vector<letter>& word){
for(unsigned i=0;i<word.size();i++)
cout<<word[i].display()<<" ";
}
//Input guess from user, return true or false
bool guess(vector<letter>&word){
char guess;//character being guessed
bool correct(false);//keeps track of whether guess is good or not
/*
//Prompt User
cout<<" Welcome to Hangman! "<<endl<<endl;
cout<<" You are allowed a total of 6 guesses "<<endl<<endl;
cout<<" If you think you know the whole word. Enter a '$' symbol"<<endl<<endl;
*/
cout<<" Guess a letter ";
cin>>guess;
guess=toupper(guess);
//Full word guessing with $ sign
if(guess=='$'){
string input;
cout<<" Guess the complete word ";
cin>>input;
//compare guess to word, return false if incorrect guess
for(unsigned i=0;i<input.length();i++){
if(toupper(input[i])!=word[i].character){
cout<<" Sorry! That is not a correct guess... "<<endl;
return false;
}
}
//if guess is correct, reveal letter and return true
for(unsigned i=0;i<word.size();i++){
word[i].hidden=false;
}
return true;
}
//if a correct letter is guessed, it is unhidden
for(size_t i=0;i<word.size();i++){
if (word[i].character==guess){
correct=true;
word[i].hidden=false;
}
}
return correct;//return whether guess correct
}
//If whole word is guessed, returns true
bool winGame(const vector<letter>&word){
for(size_t i=0;i<word.size();i++){
if(word[i].hidden)
return false;
}
return true;
}