Skip to content

Commit

Permalink
exercise 8.13.14
Browse files Browse the repository at this point in the history
  • Loading branch information
Queequeg92 committed Nov 26, 2014
1 parent a8daf47 commit 90ffc03
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ch08/ex8.13.14/data/record.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
morgan 2015552368 8625550123
drew 9735550130
lee 6095550132 2015550175 8005550000
120 changes: 120 additions & 0 deletions ch08/ex8.13.14/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/***************************************************************************
* @file main.cpp
* @author Queequeg
* @date 26 Nov 2014
* @remark This code is for the exercises from C++ Primer 5th Edition
* @note
***************************************************************************/
//!
//! Exercise 8.13
//! Rewrite the phone number program from this section to read from a named
//! file rather than from cin.

//!
//! Exercise 8.14
//! Why did we declare entry and nums as const auto &?
//! We don't expect to modify the original data.

#include<iostream>
using std::cerr;
using std::cout;
using std::cin;
using std::endl;
using std::istream;
using std::ostream;

#include<fstream>
using std::ifstream;
using std::ofstream;

#include<sstream>
using std::istringstream;
using std::ostringstream;

#include<string>
using std::string;

#include<vector>
using std::vector;

struct PersonInfo {
string name;
vector<string> phones;
};

bool read_record(istream& is, vector<PersonInfo>& people);
void format_record(ostream& os, const vector<PersonInfo>& people);

//! fake functions that make the program compile
bool valid(const string &num){ return true; }
string format(const string &num){ return num; }

int main()
{
vector<PersonInfo> people;

string filename;
cout << "Please input a record file name: ";
cin >> filename;
cout << endl;
ifstream fin(filename);

if (read_record(fin, people))
{
ofstream fout("data\\result.txt", ofstream::trunc);
format_record(fout, people);
}
else
{
cout << "Fail to open file " << filename << endl;
}

return 0;
}

bool read_record(istream& is, vector<PersonInfo>& people)
{
if (is)
{
string line, word; // will hold a line and word from input, respectively
// read the input a line at a time until cin hits end-of-file (or another error)
while (getline(is, line))
{
PersonInfo info; // create an object to hold this record's data
istringstream record(line); // bind record to the line we just read
record >> info.name; // read the name
while (record >> word) // read the phone numbers
info.phones.push_back(word); // and store them
people.push_back(info); // append this record to people
}
return true;
}
else
return false;
}

void format_record(ostream& os, const vector<PersonInfo>& people)
{
for (const auto &entry : people)
{
// for each entry in people
ostringstream formatted, badNums; // objects created on each loop
for (const auto &nums : entry.phones)
{
// for each number
if (!valid(nums))
{
badNums << " " << nums; // string in badNums
}
else
// "writes" to formatted's string
formatted << " " << format(nums);
}
if (badNums.str().empty()) // there were no bad numbers
os << entry.name << " " // print the name
<< formatted.str() << endl; // and reformatted numbers
else // otherwise, print the name and bad numbers
cerr << "input error: " << entry.name
<< " invalid number(s) " << badNums.str() << endl;
}
}

0 comments on commit 90ffc03

Please sign in to comment.