I am currently creating a Hangman program using Object oriented programming in C++. I tested my words class and it runs perfectly with a sample main that I was using. The classes that I need help with are Hangman and HangmanConsole. I am not sure what to do to get the characters, guesses, and other variables stored and in sync with the rest of the program. I am looking for help or guidance on how to code those classes so, I can get my program running. Here is my code down below:

   #include <iostream>
   #include <fstream>
   #include <ctime>
   #include <string>

      class Words
      {
      private:
        int minlen{0};
       int maxlen{0};
       std::string file_name;

        std::string* choices = nullptr;
        int count{0};

    public:

        Words (int min, int max, std::string fName)
        {
            minlen = min;
            maxlen = max;
            file_name = fName;

            this->load_words();
            srand( time(NULL) );
        }

        ~Words()
        {
            delete [] choices;
        }

        int getCount()
        {
            return count;
        }

        void display_list()
        {
            for(int i = 0; i < count; i++)
            {
                std::cout << choices[i] << '\n';
            }
        }

        void load_words()
        {
            std::ifstream fwords(file_name);

            if(!fwords)
            {
                std::cout << "Cannot open file\n";
                return;
            }
           // This counts the words (1st file read)
            std::string temp;
            count = 0;
            while(fwords >> temp)
            {
                // Filter words
                if(temp.length() == minlen or temp.length()== maxlen)
                    count++;
            }



            // Setting up the array
            choices = new std::string[count];
            fwords.close();

            // Loading the array (2nd File read)
            fwords.open(file_name);
            int index{0};
            while(fwords >> temp)
            {
                if(temp.length() == minlen or temp.length() == maxlen)
                {
                    choices[index] = temp;
                    index++;
                }
            }
            fwords.close();
        }

        std::string pick_word()
        {
            if (count == 0)
            {
                return "Zero words available to select from";
            }

            return choices[rand() % count];
        }
    };

  class Hangman
  {
   private:
      char word[40];
   //   char progress[40]; // Progress will be the same length as char_word "----"
      int word_length;
   //   void clear_progress(int length); // This will set progress to contain

     protected:
      int matches;      // Characters that match
      char last_guess;  // Final guess
      string chars_guessed; //The characters guessed
      int wrong guesses;     // Number of wrong guesses
      int user_guess;    // Character that the user will use to guess
      int remaining;     // Number of guesses remaining
      const int total_guesses = 6; // Total number of tries
      bool check(char user_guess); // Function designed to accept a single character.
 class Hangman
  {
   private:
      char word[40];
   //   char progress[40]; // Progress will be the same length as char_word "----"
      int word_length;
   //   void clear_progress(int length); // This will set progress to contain

    protected:
      int matches;      // Characters that match
      char last_guess;  // Final guess
      string chars_guessed; //The characters guessed
      int wrong guesses;     // Number of wrong guesses
      int user_guess;    // Character that the user will use to guess
      int remaining;     // Number of guesses remaining
      const int total_guesses = 6; // Total number of tries
      bool check(char user_guess); // Function designed to accept a single character.

  public:
      Hangman(char w, char p, int wlen)
      {
         word = w; // This will be used to store the generated word
         progress = p;
         word_length = wlen;

      }

      Hangman(int m, char lg, string chg, int wg, int ug, int r)
      {
         matches = m;
         last_guess = lg;
         chars_guessed = chg;
         wrong_guesses = wg;
         user_guess = ug;
         remaining = r;
      }

      char * get_word()
      {
         return word;
      }
};
 class HangmanConsole : public Hangman
 {


 };

    int main()
 {
     std::string source_file;
     source_file = "enable1.txt"; // This text file contains the complete word list for Hangman

   //  string unknown(word.length(), '-'); Maybe use this to initialize with '-' character.


     rand(time(NULL)); // needs <ctime> included
     Words words(7,10); // words between 7 and 10 chars long

     HangmanConsole game(words.pick_word());

     cout << "HANGMAN" << endl << "-------" << endl << endl;
     cout << "Your word is: " << game.get_progress() << endl;

     while (!game.is_word_complete() && game.get_remaining() > 0)
     {
        cout << endl;
        cout << "Enter your guess: ";
        cin >> game;    // calls overloaded >> operator

        system("clear"); // NON-PORTABLE, ONLY WORKS ON LINUX
        game.show_info();
 }

I'll write you are creating a Hangman program so your question is something else.

I didn't read the code because that would have me reverse engineer your design. Before we code, we have a design, sketch or more. Diving into code first, design later has so many "trying to fix this."

As to storing variables, I want to hear more about this hurdle. I looked up https://www.w3schools.com/cpp/cpp_variables.asp and it seems to be something you would learn inside the first week. Tell me more what the real issue here is.

All of the variables in your Hangman class are private or protected, meaning they are only accessible to objects of the class itself, or to objects of classes that inherit from that class.

You need methods within the Hangman class that manipulate or retreive the variables.

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.