I'm trying to load some values from a text file and output it. When it outputs, I only receive the last row on the txt file....Why is it reading in only the last line on the txt file? I made an array of classes and load into each one of them the data from the file and i want to output it. any advice helps thanks main file:
#include <iostream>
#include "membertype.h"
#include <string>
#include <iomanip>
#include <fstream>
#include <cstdlib>
memberType members[500];
int getMembersData();
void printMembersData();
int main(int argc, char** argv)
{
memberType();
getMembersData();
printMembersData();
return 0;
}
int getMembersData()
{
cout << fixed << setprecision(2);
double spent;
int bbought;
string fname, lname,id;
ifstream infile;
infile.open("project8.txt");
if(!infile)
{
cout << "unable to open input file. "<< endl;
return 1;
}
infile>>id>>fname>>lname>>bbought>>spent;
while ( !infile.eof())
{
for(int j=0;j<5;j++)
{
members[j].setMemberInfo(id,fname,lname,bbought,spent);
}
infile>>id>>fname>>lname>>bbought>>spent;
}
infile.close();
return EXIT_SUCCESS;
}
void printMembersData()
{
for(int i=0;i<5;i++)
members[i].printInfo();
}
implementation file:
#include<iostream>
#include<string>
#include"membertype.h"
using namespace std;
memberType::memberType()
{
string memberID ="non";
string firstName="emp";
string lastName="bloop";
int booksPurchased=0;
double amountSpent=0;
}
void memberType::setMemberInfo(string ID, string fName, string lName, int bPurchased, double amount)
{
memberID = ID;
firstName = fName;
lastName = lName;
booksPurchased = bPurchased;
amountSpent = amount;
}
void memberType::printInfo()
{
cout<<memberID<<firstName<<lastName<<booksPurchased<<amountSpent<<endl;
}