Using push_back for vectors

Hi everyone,

I am having some trouble trying to convert the main part of my program to use vectors. Previously, I was just using ostream and ifstream to pass data from a text file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "PhoneEntry.h"	

int main()
	{

		PhoneEntry Phone;
	
		ifstream phonenum;
		phonenum.open ("phonenum.txt");

		if (!phonenum)
    {
        perror("phonenum.txt");
        exit(1);
	}

		while (Phone.readEntry(phonenum))
		Phone.writeEntry(cout) << endl;
		return 0;
	}


I would like to do this same process using vectors, so I added #include <vector>, but I am unsure of how to use push_back and use a loop to get data and then a loop to show that data? How would I set up the first loop to access my phonenum.txt file?

Thanks for any help in advance!
Read up on std::vector<> first. http://www.cprogramming.com/tutorial/stl/vector.html

Then:
1
2
3
4
5
6
7
8
9
PhoneEntry Phone ;
ifstream phonenum( "phonenum.txt" ) ;
if( !phonenum ) { /* error */ }

std::vector< PhoneEntry > entries ;
while( Phone.readEntry(phonenum) ) entries.push_back(Phone) ;

for( std::size_t i = 0 ; i < entries.size() ; ++i ) Phone.writeEntry(cout) << '\n' ;
/* or */ for( const PhoneEntry& e : entries ) Phone.writeEntry(cout) << '\n' ; // C++11 
Thanks for your help, JLBorges. With the code that you provided me, the program prints the last entry for phonenum.txt many times. It appears that it is either accessing only one entry in the vector or storing the same values into every entry of the vector. How would I go about fixing this?

Also, I noticed that you kept my PhoneEntry Phone and added a vector. Is that necessary to make the program run with its intended function, or could I just make PhoneEntry Phone a vector?


Thanks again.
> the program prints the last entry for phonenum.txt many times.
> It appears that it is either accessing only one entry in the vector
> or storing the same values into every entry of the vector.

No, it was not printing the entries in the vector at all.
My mistake, apologies for that. Here is the (hopefully) correct code:

1
2
3
4
5
6
7
8
9
PhoneEntry Phone ;
ifstream phonenum( "phonenum.txt" ) ;
if( !phonenum ) { /* error */ }

std::vector< PhoneEntry > entries ;
while( Phone.readEntry(phonenum) ) entries.push_back(Phone) ;

// for( std::size_t i = 0 ; i < entries.size() ; ++i ) Phone.writeEntry(cout) << '\n' ;
for( std::size_t i = 0 ; i < entries.size() ; ++i ) entries[i].writeEntry(cout) << '\n' ;
Thanks JLBorges! I actually figured that out a couple minutes after I posted the message. Under this code, I am taking data from the text file, processing it through the PhoneEntry class, and then storing it in the vector, correct? I guess I thought I could make a vector of type PhoneEntry and skip making a separate vector...is that not possible?
> I guess I thought I could make a vector of type PhoneEntry and skip making a separate vector...is that not possible?

Not clear what you are getting at.

std::vector< PhoneEntry > entries ; is a vector of elements of type PhoneEntry.
Right, but I also have PhoneEntry Phone declared when the program starts. Is it possible to run this program using just the vector entries without Phone also being declared? Also, for an end of file test in this case, could I just use an if (phonenum.eof()) test?

Thanks again for all of your help!
Topic archived. No new replies allowed.