Advertisement
Sinux1

PracticeFinal(Complicated redundant unnecessary version).cpp

May 12th, 2016
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <cstdlib>
  5.  
  6. using namespace std;
  7.  
  8.  
  9. void print_list(int length)
  10. {
  11.      //step1
  12.     ifstream data_store;
  13.     int counter = 0;
  14.     //step2
  15.     data_store.open("dict.txt");
  16.     if (!data_store)
  17.     {
  18.         cout << "Error loading file\n";
  19.         exit(0);
  20.     }
  21.     else
  22.     {
  23.  
  24.         //step3
  25.         while (!data_store.eof())
  26.         {
  27.             string name;
  28.             getline(data_store, name);
  29.  
  30.  
  31.             if(name.length() == length)
  32.             {
  33.                cout << name << endl;
  34.                counter++;
  35.             }
  36.  
  37.         }
  38.  
  39.         //step4
  40.         data_store.close();
  41.  
  42.     }
  43.     cout << "Found " << counter <<" matching words\n";
  44.     return;
  45. }
  46. void is_word(int length)
  47. {
  48.     bool flag = false;
  49.  
  50.     //step1
  51.     ifstream data_store;
  52.  
  53.     //step2
  54.     data_store.open("dict.txt");
  55.     if (!data_store)
  56.     {
  57.         cout << "Error loading file\n";
  58.         exit(0);
  59.     }
  60.     else
  61.     {
  62.  
  63.         //step3
  64.         while (!data_store.eof())
  65.         {
  66.             string name;
  67.             getline(data_store, name);
  68.  
  69.             if(name.length() == length)
  70.             {
  71.                 flag = true;
  72.                 break;
  73.             }
  74.  
  75.         }
  76.  
  77.         //step4
  78.         data_store.close();
  79.  
  80.         if (flag == true)
  81.         {
  82.             print_list(length);
  83.         }
  84.         else
  85.         {
  86.             cout << "Found 0 matching words\n";
  87.         }
  88.  
  89.  
  90.     }
  91.         return;
  92. }
  93.  
  94.  
  95.  
  96. int main()
  97. {
  98.     int word_length = 15;
  99.  
  100.  
  101.     cout << "How long should the words be?\n";
  102.     //cin >> word_length;
  103.  
  104.     cout << "Words of length " << word_length << endl;
  105.     is_word(word_length);
  106.  
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement