Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem Solving With C++ 8 th. The XML (eXtensible Markup Langugage) is a common

ID: 3691231 • Letter: P

Question

Problem Solving With C++ 8 th. The XML (eXtensible Markup Langugage) is a common format used to structure and store data on the Web. The following is a small sample XML file that could be used to store names in an address book. Type it in using a text editor and save it to a file named address.xml (or find it on the accompanying CD). <?xml version="1.0" encoding="UTF-8"?> - -George Clooney1042 El Camino RealBeverly HillsCA90214 -405 A St.PalmdaleCathy PearlCA93352 -Paris Hilton200 S. Elm St.90212Beverly HillsCA -CA93354Wendy Jones982 Boundary Ave.Palmdale a. You are hosting a party in Palmdale, CA. Write a program that reads in the address.xml file and outputs the names and addresses of everyone in Palmdale.Your program shouldn't output any of the tag information, just the address content. b. You would like to send an advertising flyer to everyone in zip codes 90210 through 90214. Write a program that reads in the address.xml file and outputs the names and addresses of everyone whose zip code falls within the specified range. You may assume that each contact in the address file has the same structure and the same fields. However, your solution should be able to handle an input file with any number of contacts and should not assume that the fields within each contact are in the same order. How to write code without using vector for question b?

Explanation / Answer

main.cpp
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
void change_2_easy_getA();
void change_2_easy_getB();
string CITY="Palmdale";
int CODE_S=90210,CODE_E=90214;
int main()
   {
       cout<<"A:City is Palmdale ";
   change_2_easy_getA();
       cout<<"B:Zip Code is 90210-90214 ";
   change_2_easy_getB();
   return 0;
   }
void change_2_easy_getA()//ok
   {
   ifstream fin;
   fin.open("Addresses.xml");
   string t, name, street, city, state, zip;
   int first_time=2, line_no=1, first_time_city=1;
   while(!fin.eof())
       {
       if(first_time)
           {
           getline(fin,t);
           first_time--;
           }
       else
           {
           if(line_no==1)
               {getline(fin,t);}
           if(line_no==2)
               {getline(fin,name);}
           if(line_no==3)
               {getline(fin,street);}
           if(line_no==4)
               {getline(fin,city);}
           if(line_no==5)
               {getline(fin,state);}
           if(line_no==6)
               {getline(fin,zip);}
           if(line_no==7)
               {getline(fin,t);
               line_no=0;
               }
           line_no++;

           //A
          
           if(city.find(CITY)==12&&line_no==7)
               {
               int n_pos, s_pos, c_pos, st_pos, z_pos;
               int n_pos1, s_pos1, c_pos1, st_pos1, z_pos1;
               n_pos=name.find(">")+1;
               s_pos=street.find(">")+1;
               c_pos=city.find(">")+1;
               st_pos=state.find(">")+1;
               z_pos=zip.find(">")+1;

               n_pos1=name.find("</");
               s_pos1=street.find("</");
               c_pos1=city.find("</");
               st_pos1=state.find("</");
               z_pos1=zip.find("</");
              

  
               string t_name, t_street, t_city, t_state, t_zip;
               t_name=name.substr(n_pos, n_pos1-n_pos);
               t_street=street.substr(s_pos, s_pos1-s_pos);
               t_city=city.substr(c_pos, c_pos1-c_pos);
               t_state=state.substr(st_pos, st_pos1-st_pos);
               t_zip=zip.substr(z_pos,z_pos1-z_pos);
          
               cout<<t_name<<" "<<t_street<<" "<<t_city<<" "<<t_state<<" "<<t_zip<<" ";
               city="";              

               }

  
           }
       }


   fin.close();
  
   }


   void change_2_easy_getB()//ok
   {
   ifstream fin;
   fin.open("Addresses.xml");
   string t, name, street, city, state, zip;
   int first_time=2, line_no=1,tt_zip=0;
   while(!fin.eof())
       {
       if(first_time)
           {
           getline(fin,t);
           first_time--;
           }
       else
           {
           if(line_no==1)
               {getline(fin,t);}
           if(line_no==2)
               {getline(fin,name);}
           if(line_no==3)
               {getline(fin,street);}
           if(line_no==4)
               {getline(fin,city);}
           if(line_no==5)
               {getline(fin,state);}
           if(line_no==6)
               {
               getline(fin,zip);

               int s1;
               s1=zip.find(">")+1;
               ofstream fou;
               fou.open("2.txt");
               fou<<zip.substr(s1,5);
               fou.close();
               ifstream fin;
               fin.open("2.txt");
               int tt;
               fin>>tt;
               tt_zip=tt;
               fin.close();

               }
           if(line_no==7)
               {getline(fin,t);
               line_no=0;
               }
           line_no++;
      
           for(int i=CODE_S; i<CODE_E+1;i++)
               {
          
           //if(zip.find("90210")==11||zip.find("90211")==11||zip.find("90212")==11||zip.find("90213")==11||zip.find("90214")==11)
               if(tt_zip>CODE_S-1&&tt_zip<CODE_E+1)
                   {
               int n_pos, s_pos, c_pos, st_pos, z_pos;
               int n_pos1, s_pos1, c_pos1, st_pos1, z_pos1;
               n_pos=name.find(">")+1;
               s_pos=street.find(">")+1;
               c_pos=city.find(">")+1;
               st_pos=state.find(">")+1;
               z_pos=zip.find(">")+1;

               n_pos1=name.find("</");
               s_pos1=street.find("</");
               c_pos1=city.find("</");
               st_pos1=state.find("</");
               z_pos1=zip.find("</");
              

  
               string t_name, t_street, t_city, t_state, t_zip;
               t_name=name.substr(n_pos, n_pos1-n_pos);
               t_street=street.substr(s_pos, s_pos1-s_pos);
               t_city=city.substr(c_pos, c_pos1-c_pos);
               t_state=state.substr(st_pos, st_pos1-st_pos);
               t_zip=zip.substr(z_pos,z_pos1-z_pos);
          
               cout<<t_name<<" "<<t_street<<" "<<t_city<<" "<<t_state<<" "<<t_zip<<" ";
               zip="";
               tt_zip=0;

               }
               }
      
           }
       }


   fin.close();
  
   }
  
  
   sample output
A:City is Palmdale                                                                                                                                          
                                                                                                                                                            
B:Zip Code is 90210-90214                                                                                                                                   
                                                                                                                                                            

  
   

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote