Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <fstream>
- using namespace std;
- struct person {
- string firstname;
- string lastname;
- string phonenumber;
- void clear() {
- firstname.clear();
- lastname.clear();
- phonenumber.clear();
- }
- size_t length() {
- return firstname.length() +
- lastname.length() +
- phonenumber.length();
- }
- };
- int main() {
- ifstream inputfile;
- vector<person> directory;
- inputfile.open("phonedata.txt");
- if (inputfile.is_open()) {
- person per;
- vector<person>::iterator it;
- int count = 0;
- while (!inputfile.eof()) {
- per.clear();
- inputfile >> per.firstname >> per.lastname >> per.phonenumber;
- if (per.length()) {
- it = directory.begin();
- directory.insert(it + count++, per);
- }
- }
- inputfile.close();
- for (int i=0; i < directory.size(); i++) {
- cout << directory[i].firstname << " "
- << directory[i].lastname << "\t"
- << directory[i].phonenumber << "\n";
- }
- }
- else {
- cout << "Failed to open input file.\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement