Advertisement
Sinux1

3/15/16 class examples T5E11

Mar 15th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream> // ifstream, open, eof, close
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     // step 1
  8.     ifstream data_store;
  9.     string filename;
  10.  
  11.     cout << "What is the name of the filename?" << endl;
  12.     getline(cin, filename);
  13.  
  14.     // step 2
  15.     data_store.open(filename.c_str());
  16.  
  17.     // step 3
  18.     if (!data_store) // If any problem with file
  19.     {
  20.         cout << "Fatal Error: Can not find " << filename << "." << endl;
  21.         return 0;
  22.     }
  23.  
  24.     string data;
  25.     int num_found = 0;
  26.  
  27.     while (!data_store.eof()) // While we are not at the end of file
  28.     {
  29.         data_store >> data; // Reading a word from the data store (not the keyboard)
  30.         if (data == "Romeo" || data == "Romeo's" || data == "Romeo." || data == "Romeo?" || data == "Romeo,"
  31.              || data == "Romeo!")
  32.         {
  33.             num_found++;
  34.         }
  35.         cout << data << endl;
  36.     }
  37.  
  38.     cout << "Found " <<  num_found << " cases of Romeo" << endl;
  39.  
  40.     // step 4
  41.     data_store.close();
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement