0

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;
}
WhozCraig
  • 59,130
  • 9
  • 69
  • 128
john
  • 19
  • 4
  • http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – Neil Kirk Nov 14 '14 at 00:18
  • Don't hardset an array to an arbitrary size. Use a vector and set it to the size you need. – Neil Kirk Nov 14 '14 at 00:19
  • Why do you copy the strings twice in `setMemberInfo`? Pass them by const reference. – Neil Kirk Nov 14 '14 at 00:19
  • so them major problem is the eof loop, i cant use a loop inside it? thatll fix it? – john Nov 14 '14 at 00:31
  • ok so it did fix it lol. is there a way i can go back to the top of the file? – john Nov 14 '14 at 00:33
  • Possibly, I didn't look too closely. I just spotted that. Try `while (infile>>id>>fname>>lname>>bbought>>spent) {..}` – Neil Kirk Nov 14 '14 at 00:33
  • is there a way to start backup top of the file? – john Nov 14 '14 at 00:33
  • `infile.seekg(0, ios::beg);` – Neil Kirk Nov 14 '14 at 00:36
  • you should've madey our comment into an answer, i'd +1 – john Nov 14 '14 at 00:43
  • 2
    You should search the web and StackOverflow for "c++ read text columns". My issue is that there are soooooo many that each one is slightly different so they can't be marked as duplicates. :-( – Thomas Matthews Nov 14 '14 at 01:23
  • 1
    I concur with Thomas. This code is *littered* with either problems, potential problems, and/or useless activities it is impossible to pin it down to a single duplicate, though there are plenty of duplicates of each problem present. Were I to offer any advice it would be to rewind the clock and start with something **basic**, making small incremental changes with each new feature addition. – WhozCraig Nov 14 '14 at 01:27

0 Answers0