how do i use vectors in this situation

it's outputting fine but i dont know how to only output addresses that are in "Palmdale"

i was told to use vectors but i dont know how vectors would apply to this problem

the program is meant to read only addresses in Palmdale

this is what the xml file looks like, it displays 4 addresses

<address_book>

- <contact>
<name>George Clooney</name>
<street>1042 El Camino Real</street>
<city>Beverly Hills</city>
<state>CA</state>
<zip>90214</zip>
</contact>

- <contact>
<name>Cathy Pearl</name>
<street>405 A St.</street>
<city>Palmdale</city>
<state>CA</state>
<zip>93352</zip>
</contact>

- <contact>
<name>Paris Hilton</name>
<street>200 S. Elm St.</street>
<city>Beverly Hills</city>
<state>CA</state>
<zip>90212</zip>
</contact>

- <contact>
<name>Wendy Jones</name>
<street>982 Boundary Ave.</street>
<city>Palmdale</city>
<state>CA</state>
<zip>93354</zip>
</contact>

</address_book>

this is my code so far

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;

void intro();
void address(ifstream& fin, string& line);

int main()
{
    string line;
    ifstream fin;

    fin.open("address.xml");

    if(fin.fail()) //opens the file
    {
        cout << "Opening file failed \n";
        exit(1);
    }

    intro();
    address(fin, line); //this is my function call

    return 0;
}

void intro()
{
    cout << "This program will output addresses only located in Palmdale \n";
}

void address(ifstream& fin, string& line) //the function that outputs the addresses
{
    int pos1, pos2;

    while(getline(fin, line))
    {
        if( (pos1 = line.find("<name>")) < string::npos) //string::npos indicates the length of the string
        {
            pos1 = line.find(">"); //this locates the numered location of <name>
            pos2 = line.find("</name>"); //locates the numbered location of </name>

            cout << endl << line.substr(pos1 + 1, pos2 - pos1 - 1) << endl;
            //the function line.substr outputs what is in between of the memory location of pos1 and pos2
            //pos + 1 reads the first output after <name>
            //pos2 - pos - 1 cancels the +1 to pos1
        }

        if( (pos1 = line.find("<street>")) < string::npos)
        {
            pos1 = line.find(">");
            pos2 = line.find("</street>");

            cout << line.substr(pos1 + 1, pos2 - pos1 - 1) << endl;
        }

        if( (pos1 = line.find("<city>")) < string::npos)
        {
            pos1 = line.find(">");
            pos2 = line.find("</city>");

            cout << line.substr(pos1+ 1, pos2 - pos1 - 1) << endl;
        }

        if( (pos1 = line.find("<state>")) < string::npos)
        {
            pos1 = line.find(">");
            pos2 = line.find("</state>");

            cout << line.substr(pos1 + 1, pos2 - pos1 - 1) << endl;
        }
        if( (pos1= line.find("<zip>")) < string::npos)
        {
            pos1 = line.find(">");
            pos2 = line.find("</zip>");

            cout << line.substr(pos1 + 1, pos2 - pos1 - 1) << endl;
        }
    }
}


my output is



This program will output addresses only located in Palmdale

George Clooney
1042 El Camino Real
Beverly Hills
CA
90214

Cathy Pearl
405 A St.
Palmdale
CA
93352

Paris Hilton
200 S. Elm St.
Beverly Hills
CA
90212

Wendy Jones
982 Boundary Ave.
Palmdale
CA
93354

Process returned 0 (0x0)   execution time : 0.090 s
Press any key to continue.



my deepest regards, thankss
Last edited on
Hi

create class of Address , i.e.

1
2
3
4
5
6
7
struct Address{
	string name;
	string street;
	string city;
	string state;
	string zip;
};


change your address function as
void address(ifstream& fin, string& line, vector<Address>& datas);

create an object of type vector<Address> datas in your main and call the address from main as address(fin, line, datas);

in your address function in while loop create and object of type Address
, Address add, and replace each cout with add.memberName = line.substr(pos1 + 1, pos2 - pos1 - 1)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
while(getline(fin, line))
    {
        
     Address add;
		if( (pos1 = line.find("<name>")) < string::npos) //string::npos indicates the length of the string
        {
            pos1 = line.find(">"); //this locates the numered location of <name>
            pos2 = line.find("</name>"); //locates the numbered location of </name>
			add.name = line.substr(pos1 + 1, pos2 - pos1 - 1);
           
        }

// the same fo other contitions
     {
     }
    /*
    ......
    */
	datas.push_back(add);
    }
}


}


and finally, in your main go through an loop and print the datas for which datas[i].city=="Palmdale" is

hope it helps
Last edited on
can we not write ...

if( (pos1 = line.find("<name>")) < string::npos)
as
if( (pos1 = line.find("<name>")) != string::npos)
just a suggetion

cheers
vega
Topic archived. No new replies allowed.