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");
        int count = 0;
        if (fin.is_open()) {
            string word;

            while (!fin.eof()) {
                word = fin.get();
                if (word.length() >= minlen && word.length() <= maxlen) {
                    if (fin.good()) {
                        count++;
                    }
                }
            }
        }
        fin.close();
        return count;
    }
    void load_words()
    {
        string* choices = new string[count];

        ifstream fin;

        fin.open("enable1.txt");
        int count = 0;
        if (fin.is_open()) {
            string word;

            while (!fin.eof()) {
                word = fin.get();
                if (word.length() >= minlen && word.length() <= maxlen) {
                    if (fin.good()) {
                        for (int i = 0; i < count; i++) {
                            choices[i] = word;
                        }
                    }
                }
            }
            fin.close();
        }
    }

public:
    Words(int max, int min)
    {
        minlen = min;
        maxlen = max;
    }
    string pick_word()
    {
        if (count == 0) {
            return " ";
        }
        else {
            srand(time(0));
            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;
}

I've complied it, and it works but I'm getting a segmentation fault

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.

I want to get an out put like:

[Run your program]
Enter min: 27
Enter max: 29
electroencephalographically
David G
  • 94,763
  • 41
  • 167
  • 253
  • The file just contains a bunch of random words from line 1 to 999. – WyRm ACEN H08 May 04 '21 at 03:13
  • 1
    `string * choices = new string[count];` this declares a new local variable `choices` which means your class member variable called `choices` is never initialised – The Dark May 04 '21 at 03:15
  • OT: Delete `srand(time(0));` from `pick_word()` you should not reseed the random number generator every time you pick a random number. – drescherjm May 04 '21 at 03:15
  • 2
    and also load_words is never called – The Dark May 04 '21 at 03:16
  • Why would you allocate for `choiices` to begin with. Is `std::string` not good enough? – David C. Rankin May 04 '21 at 03:19
  • Neither `count` nor `choices` is initialized. – Dmitry Kuzminov May 04 '21 at 03:34
  • `word = fin.get();` doesn't read a whole word. Read the comments on your last question. If you check the return values for actual input and output operations you won't have to sprinkle `fin.good()` and `fin.eof()` everywhere. If you used a `std::vector` you wouldn't have to worry about dynamic allocation either. – Retired Ninja May 04 '21 at 03:36
  • I appreciate your consideration/guidance/help/time. I’ve got it to work. – WyRm ACEN H08 May 04 '21 at 03:36
  • 1
    The first thing you want to read is this. https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons – n. m. could be an AI May 04 '21 at 04:57

0 Answers0