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:
#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 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?
> 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:
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?
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?