#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