0
   #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;
              fin.open("enable1.txt ");
              count = 0;
              if (fin.is_open())
              {
                  string word;
 
                  while (!fin.eof())
                  {
                      getline(fin, word);
                      if (fin.good())
                      {
                          if (word.length() >= minlen && word.length() <= maxlen)
                          {
                              count++;
                          }
                      }
 
 
 
 
                  }
 
              }
              fin.close();
              return count;
          }
          void load_words()
          {
              int index = 0;
              choices = new string[count];
              ifstream fin;
              fin.open("enable1.txt ");
              if (fin.is_open())
              {
                  string word;
 
                  while (!fin.eof())
                  {
                      getline(fin, word);
                      if (fin.good())
                      {
                          if (word.length() >= minlen && word.length() <= maxlen)
                          {
                              choices[index] = word;
                              index++;
                          }
 
 
 
                      }
 



                  }
                  fin.close();
             }
 
 
          }
      public:
          Words(int max, int min)
          {
              minlen = min;
              maxlen = max;
              count_candidates();
              load_words();
          }
          string pick_word()
          {
              if(count == 0)
              {
                  return " ";
              }
              else
              {
                  return choices[rand() % count];
              }
 
          }
          ~Words()
         {
             if (choices != NULL)
             {
                 delete [] choices;
             }
         }




 };


 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;

     return 0;

 }

I'm trying to read a text file that was created that has a variety of words from line 1 to 999.

for the text file open PowerShell and put cp /home/fac/mmanibo/shared/enable1.txt . I don't know if I can share this outside my domain.

When I run this code, it asks for user input, but I do not get an output.

I want to get an output like:

[Run your program]
Enter min: 27
Enter max: 29
electroencephalographically

My output

  • You've ducked [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) by brute force. A more elegant solution is Use `while (getline(fin, word)) {if (word.length() >= minlen && word.length() <= maxlen) { count++; } }` – user4581301 May 06 '21 at 01:07
  • 1
    Consider printing out a message if the file fails to open. That's the most likely cause. – user4581301 May 06 '21 at 01:10
  • 1
    The `count_candidates` and `load_words` are doing basically the same thing. If you modify your code to use a `std::vector` instead you don't really need the `count_candidates` function. – Some programmer dude May 06 '21 at 01:10
  • `std::vector` is a self-sizing dynamic array. You read from the file, `push_back` what you read into the `vector`, and it does all of the rest of the work making sure the `vector`'s the right size and cleaned up on schedule. It doesn't get much easier than `vector`, and `vector` is almost always much, much easier than trying to manage the array yourself. [Documentation link](https://en.cppreference.com/w/cpp/container/vector) – user4581301 May 06 '21 at 01:27
  • I’ll try it out – WyRm ACEN H08 May 06 '21 at 01:41

0 Answers0