I am trying to figure out how to modify this program so the file is read from the constructor:

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


class Scramble { 
      private: 
               vector<string> _word;
               bool Check (string T, string S); 
      public: 
              Scramble(vector<string> words){  
              _word=words; 
              
} 
void Descramble (string Scrambled); 
}; 
//******************************************
void Scramble::Descramble(string Scrambled)
{ 
      bool flag=false; 
      for(int i=0;i<_word.size();i++)
      { 
              if(_word[i].size()==Scrambled.size())
              {
                   if(Check(Scrambled,_word[i])) 
                   {
                        cout << "The word is: " << _word[i]<< endl; 
                        flag=true;}
              }
      }
      if (!flag)
      {
              cout<< "The word: " << Scrambled <<" is not found"<< endl; 
              return;
      }
}
//******************************************
bool Scramble::Check(string T, string S)
{ 
      int n=T.size(); 
      int Found=0; 
      string temp=S; 
      for (int i=0; i<n; i++) 
      { 
              bool flag = true; 
              for (int j=0; j < n && flag;j++) 
              if(T[i]== temp[j])
              { 
                   temp[j]=' ';
                   flag=false;Found ++;
              } 
      }
      if (Found==n)
              return true; 
      else 
              return false;
} 
//******************************************
int main(){ 
      cout << "Welcome to the Scramble Game!\n\n";
      while (true)
      {
              int play;
              cout << "\nWould you like to play? \nEnter 1 for Yes or 2 for No:\n";
              cin >> play;
              if (play==1)
              {
                   cout <<"\nPlease enter the scrambled word: ";
                   string Scrambled ;
                   cin >> Scrambled; 
                   ifstream file("dictionary.txt");
               if(!file)
                   {
	                    cout <<"\nError! The file couldn't be opened\n";
                   }
                    vector<string> word_list;
                   string word;
                   while(file >> word)
	               {
                        word_list.push_back(word);
                   }
                       Scramble test(word_list); 
                   test.Descramble(Scrambled);
                   file.close();
                   cin.get(); cin.get();
                  
                  
              
              } 
              else if (play==2)
              {
                   cout << "\n\nGoodbye!\n\n";
                   cin.get(); cin.get();
                   return 0;
              }
              else
              {
                   cout << "\nInvalid entry. Try again.\n\n";
                   cin.get(); cin.get();
              }
      }
}

Going off the only example in my book that comes even close to what I need, I have created this horrible thing:

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


class Scramble { 
      private: 
               vector<string> _word;
               bool Check (string T, string S); 
      public: 
              Scramble(fstream &file, string Scrambled){  
                                       if(!file)
                   {
	                    cout <<"\nError! The file couldn't be opened\n";
                   }
                   vector<string> word_list;
                   string word;
                   while(file >> word)
	               {
                        word_list.push_back(word);
                   }
      
              
} 
void Descramble (string Scrambled); 
}; 
//******************************************
void Scramble::Descramble(string Scrambled)
{ 
      bool flag=false; 
      for(int i=0;i<_word.size();i++)
      { 
              if(_word[i].size()==Scrambled.size())
              {
                   if(Check(Scrambled,_word[i])) 
                   {
                        cout << "The word is: " << _word[i]<< endl; 
                        flag=true;}
              }
      }
      if (!flag)
      {
              cout<< "The word: " << Scrambled <<" is not found"<< endl; 
              return;
      }
}
//******************************************
bool Scramble::Check(string T, string S)
{ 
      int n=T.size(); 
      int Found=0; 
      string temp=S; 
      for (int i=0; i<n; i++) 
      { 
              bool flag = true; 
              for (int j=0; j < n && flag;j++) 
              if(T[i]== temp[j])
              { 
                   temp[j]=' ';
                   flag=false;Found ++;
              } 
      }
      if (Found==n)
              return true; 
      else 
              return false;
} 
//******************************************
int main(){ 
      cout << "Welcome to the Scramble Game!\n\n";
      while (true)
      {
              int play;
              cout << "\nWould you like to play? \nEnter 1 for Yes or 2 for No:\n";
              cin >> play;
              if (play==1)
              {
                   cout <<"\nPlease enter the scrambled word: ";
                   string Scrambled ;
                   cin >> Scrambled; 
                   ifstream file("dictionary.txt");
                   word_list=Scramble (file, Scrambled);
                    word_list.Scramble::Descramble(Scrambled);
                   file.close();
                   cin.get(); cin.get();
              } 
              else if (play==2)
              {
                   cout << "\n\nGoodbye!\n\n";
                   cin.get(); cin.get();
                   return 0;
              }
              else
              {
                   cout << "\nInvalid entry. Try again.\n\n";
                   cin.get(); cin.get();
              }
      }
}

How do I pass a file or filename to a constructor? And how do I retrieve "word_list" if I can't return from a constructor?

> How do I pass a file or filename to a constructor?
write a constructor that takes a file name as the argument.

> And how do I retrieve "word_list" if I can't return from a constructor?
let the constructor store the words in a member variable. and provide an accessor function to retrieve the member.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

struct A
{
  explicit A( const std::string& file_name ) ;
  const std::vector< std::string >& word_list() const ;
  // ...

  private: std::vector< std::string > _word_list ;
  // ...
};

A::A( const std::string& file_name )
{
  std::ifstream file( file_name.c_str() ) ;
  std::string word ;
  while( file >> word ) _word_list.push_back(word) ;
}

const std::vector< std::string >& A::word_list() const
{ return _word_list ; }

int main()
{
  A object( __FILE__ ) ;
  const std::vector< std::string >& wl = object.word_list() ;
  // use wl
}
This article has been dead for over six months. Start a new discussion instead.