Opening a file within a private class

  c++
#include <iostream>
#include <ctime>
#include <fstream>
#include <cstdlib>

using namespace std;

class Words
{
  private:
      int minlen;
      int maxlen;
      int count;
      string * choices;

      int count_candidates()
      {
          ifstream fin("enable1.txt");
          int word = 1;
          char ch;
          fin.seekg(0,ios::beg); //bring position of file pointer to begining of file

          while(fin)
          {
              fin.get(ch);
              if(ch==' '||ch=='n')
                  word++;
          }

        //  cout<<"nWords="<<word<<"n";
          fin.close(); //closing file

          return 0;
      }


  public:

};

int main()
{
   srand(time(NULL));  // needs <ctime> included
   int min, max;

   cout << "Enter min: ";
   cin >> min;

   cout << "Enter max; ";
   cin >> max;

   Words words(min, max);

   cout << words.pick_word() << endl;
}

I want Create a private function int count_candidates(). Which will open the file called enable1.txt (this file has random words from line 1 to 999 and I know you can’t use the file for reference)and count all the words in the file whose lengths are between minlen and maxlen. This function will return the resulting count. If you use a string to load the words from the file, you can use its public function .length() to determine the word’s length.

This an incomplete code so I don’t think you can get an output with this.

Source: Windows Questions C++

LEAVE A COMMENT