Sinux1

T7E5.cpp

Apr 9th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream> // ifstream, open, eof, close
  3. #include <cstdlib> // rand, srand, exit
  4. #include <ctime> // time
  5. using namespace std;
  6.  
  7.  
  8. void load_dictionary(string word_list[])
  9. {
  10.     //step 1
  11.     ifstream data_store;
  12.  
  13.     //step 2
  14.     data_store.open("dict.txt");
  15.  
  16.     //step 3
  17.     int subscript = 0;
  18.  
  19.     if (!data_store)
  20.     {
  21.         cout << "Could not find dict.txt in this directory" << endl;
  22.         exit(0);
  23.     }
  24.  
  25.     while(!data_store.eof())
  26.     {
  27.         string word;
  28.         getline(data_store, word);
  29.  
  30.         word_list[subscript] = word;
  31.         subscript++;
  32.     }
  33.     //step 4
  34.     data_store.close();
  35. }
  36.  
  37. string get_random_word(string word_list[],int size)
  38. {
  39.     int random_num = rand() % size;
  40.     string the_word = word_list[random_num];
  41.  
  42.     return the_word;
  43.  
  44. }
  45.  
  46. int main()
  47. {
  48.  
  49.     srand(time(NULL));
  50.  
  51.     string word_list[21882];
  52.     int input;
  53.     load_dictionary(word_list);
  54.  
  55.     cout << "Hey user, how many random words would you like to generate?" << endl;
  56.     cin >> input;
  57.  
  58.     for (int counter = 1; counter <= input; counter++)
  59.     {
  60.         string result = get_random_word(word_list, 21882);
  61.         cout << result << endl;
  62.     }
  63.  
  64.  
  65.     return 0;
  66. }
Add Comment
Please, Sign In to add comment