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;
}

line 136: I don't understand the purpose of that calculation. I would think the fastest and easiest way to get a random word from the text file is to first read the entire file into a vector of std::strings, then the program will always know how may words there are. After that, it's a trivel calculation, generate a random number between 0 and the number of strings in the vector. No loops are needed for that. Read the file into a vector when the program first starts so that it doesn't have to read the file each time a random word is needed.

Of course I'm assuming the file is rather small, 1,000 (+/-) or fewer words. If the file is really huge then there are other alternatives that may be better than reading it all into memory at the same time.

There are more than 2000 words in that file?

Anything more you can give me for help, I've spent that last 2 hours searching on trying to read in the file and choosing a random one with rand. I'm completely stuck.

something like below is what I have in mind. 2000 words should probably not be a problem.

#include <vector>
#include <string>
#include <fstream>
#include <iostream>
#include <ctime>
using namespace std;

int main()
{
    vector<string> words;
    ifstream in("filename.txt");
    if (!in.is_open())
    {
        cout << "Error opening file\n";
        return 1;
    }
    srand((unsigned int) time(0)); // seed the random number generator
    string word;
    while (in >> word)
        words.push_back(word);
    in.close();
    int n = rand() % word.size(); // get a random word
    string random_word = words[n];
    cout << "Random word is \"" << random_word << "\"\n";
    return 0;
}

Why are you converting the random word into a vector of single characters? It would be a lot simpler to just index into the std::string array.

Thank You.

Would that be something I would have to use in main or emulate it into the getword function which is what I'm going to try to do?

Edit: I thought it would just be easier that way to be able to display the blank spaces of the secret word. I'm still learning c++

The only thing you need in getWord() is lines 22 and 23 of the code I posted. All the rest is just one-time initialization of the words vecor and can be put at the beginning of main().

You might want to pass the vector of words as a parameter to getWord()

As you can see, getWord() is nothing more than a one-line function.

std::string getWord(vector<string>& words)
{
   return words[rand() % words.size()];
}

Everything looks good. I just have 2 errors one with
'word' being an undeclared identifier on line 22
and a class/struct/function being needed to the left of .size

'word' being an undeclared identifier on line 22

Check the spelling, it's probably not the same spelling as the parameter.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.