#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