Advertisement
NyteOwlDave

Simple Phone List 2

Feb 9th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. struct person {
  8.   string firstname;
  9.   string lastname;
  10.   string phonenumber;
  11.   void clear() {
  12.     firstname.clear();
  13.     lastname.clear();
  14.     phonenumber.clear();
  15.   }
  16.   size_t length() {
  17.     return firstname.length() +
  18.            lastname.length() +
  19.            phonenumber.length();
  20.   }
  21. };
  22.  
  23. int main() {
  24.  
  25.   ifstream inputfile;
  26.   vector<person> directory;
  27.  
  28.   inputfile.open("phonedata.txt");
  29.  
  30.   if (inputfile.is_open()) {
  31.  
  32.     person per;
  33.     vector<person>::iterator it;
  34.     int count = 0;
  35.  
  36.     while (!inputfile.eof()) {
  37.  
  38.       per.clear();
  39.  
  40.       inputfile >> per.firstname >> per.lastname >> per.phonenumber;
  41.  
  42.       if (per.length()) {
  43.         it = directory.begin();
  44.         directory.insert(it + count++, per);  
  45.       }
  46.     }
  47.  
  48.     inputfile.close();
  49.  
  50.     for (int i=0; i < directory.size(); i++) {
  51.       cout << directory[i].firstname << " "
  52.           << directory[i].lastname << "\t"
  53.           << directory[i].phonenumber << "\n";
  54.     }
  55.   }
  56.   else {
  57.     cout << "Failed to open input file.\n";
  58.   }
  59.    
  60.   return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement