Search code examples
c++filefile-iostructurecharacter-arrays

Program won't read past the first entry in file


I need help fixing this C++ program that is not writing the second entry in one file to another file. It only seems to be writing the first entry only to the file and then after that the while loop is terminating, even though the end of the file has not been reached. Here is the program:

indata.open("income.dat", ios::out | ios::binary);

while (true)
{
    indata.ignore();
    indata.getline(person[count2].name, NAMESIZE); 
    indata >> person[count2].income;
    indata >> person[count2].rent;
    indata >> person[count2].food;
    indata >> person[count2].utilities;
    indata >> person[count2].miscell;

    if (count2 == 0)
    {
        outdata << setw(20) << "Name"
                << setw(10) << "Income"
                << setw(10) << "Rent" 
                << setw(10) << "Food"
                << setw(15) << "Utilities"
                << setw(15) << "Miscellaneous"
                << setw(10) << "Net Money" << endl << endl;
    }

    outdata << setw(20) << person[count2].name 
            << setw(10) << person[count2].income 
            << setw(10) << person[count2].rent 
            << setw(10) << person[count2].food 
            << setw(15) << person[count2].utilities 
            << setw(15) << person[count2].miscell 
            << setw(10) << person[count2].net << endl;

    count2++;
}

outdata.close();

Conditions I have tested in the while loop include, indata, !indata.eof(), and indata.good().

Any thoughts? Thanks.


Solution

  • I solved this problem:

    I couldn't figure out how to get the while loop to iterate more than once for this particular case, so I used a for loop:

    indata.open("income.dat", ios::out | ios::binary);
    
       for (count2 = 0 ; count2 < count + 1 ; count2++)
       {
        indata.ignore();
        indata.getline(person[count2].name, '\n'); 
        indata >> person[count2].income;
        indata >> person[count2].rent;
        indata >> person[count2].food;
        indata >> person[count2].utilities;
        indata >> person[count2].miscell;
        indata >> person[count2].net;
    
        if (count2 == 0)
        {   
            // write information to output file
            outdata << setw(20) << "Name" << setw(10) << "Income" << setw(10) << "Rent" 
                << setw(10) << "Food" << setw(15) << "Utilities" << setw(15)
                    << "Miscellaneous" << setw(10) << "Net Money" << endl << endl;
        }
    
        outdata << setw(20) << person[count2].name 
                    << setw(10) << person[count2].income 
                    << setw(10) << person[count2].rent 
                    << setw(10) << person[count2].food 
                    << setw(15) << person[count2].utilities 
                    << setw(15) << person[count2].miscell 
                    << setw(10) << person[count2].net << endl;
       }
    
       outdata.close();
    

    For this particular case, use count from a previous instance as a condition for the for loop.