Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream> // ifstream, open, eof, close
- #include <cstdlib> // rand, srand, exit
- #include <ctime> // time
- using namespace std;
- void load_dictionary(string word_list[])
- {
- //step 1
- ifstream data_store;
- //step 2
- data_store.open("dict.txt");
- //step 3
- int subscript = 0;
- if (!data_store)
- {
- cout << "Could not find dict.txt in this directory" << endl;
- exit(0);
- }
- while(!data_store.eof())
- {
- string word;
- getline(data_store, word);
- word_list[subscript] = word;
- subscript++;
- }
- //step 4
- data_store.close();
- }
- string get_random_word(string word_list[],int size)
- {
- int random_num = rand() % size;
- string the_word = word_list[random_num];
- return the_word;
- }
- int main()
- {
- srand(time(NULL));
- string word_list[21882];
- int input;
- load_dictionary(word_list);
- cout << "Hey user, how many random words would you like to generate?" << endl;
- cin >> input;
- for (int counter = 1; counter <= input; counter++)
- {
- string result = get_random_word(word_list, 21882);
- cout << result << endl;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment