1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
struct Contact
{
string name, street, city, state, zip;
};
vector<Contact> contacts;
void parse_tags(const string & row, const string & begintag, const string &endtag, string & dest)
{
int start, end;
dest.clear();
start = row.find(begintag);
if (start != string::npos)
{
start = row.find(">");
end = row.find(endtag);
dest = row.substr(start + 1, end - start - 1);
}
}
void print_contact(const Contact & contact)
{
cout << contact.name << endl << contact.street << endl
<< contact.city << endl << contact.state << endl << contact.zip << endl << endl;
}
int main () {
ifstream fin;
Contact temp;
string row;
fin.open("address.xml");
if (fin.fail()) {
cout << "There was an error opening the file...";
exit(1);
}
while (getline(fin, row))
{
parse_tags(row, "<name>", "</name>", temp.name);
parse_tags(row, "<street>", "</street>", temp.street);
parse_tags(row, "<city>", "</city>", temp.city);
parse_tags(row, "<state>", "</state>", temp.state);
parse_tags(row, "<zip>", "</zip>", temp.zip);
if ((row.find("</contact>")) != string::npos)
contacts.push_back(temp);
}
fin.close();
cout << "\nHere are your contacts who live in Palmdale:\n\n";
for (size_t i = 0; i < contacts.size(); i++)
if (contacts[i].city == "Palmdale")
print_contact(contacts[i]);
cout << "\nHere are your contacts who live in zip codes 90210-90214:\n\n";
for (size_t i = 0; i < contacts.size(); i++)
if (contacts[i].zip >= "90210" && contacts[i].zip <= "90214")
print_contact(contacts[i]);
return 0;
}
| |