0

I am a beginner programer trying to learn C++. I am trying to read the following information from a file:

Dean DeFino 88 98 99
Sally Johnson 78 89 82
Bill Benny 75 79 81
Thomas Billows 78 84 89

However, I get the following output and I can't figure out why:

Dean DeFino 88 98 99
0 0 0
Sally Johnson 78 89 82
0 0 0

Here is the code:

#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;

const int NAMESIZE = 15;
const int MAXRECORDS = 50;
struct Grades                             // declares a structure
{
    char name[NAMESIZE + 1];
    int test1;
    int test2;
    int final;

};

typedef Grades gradeType[MAXRECORDS];    
 // This makes gradeType a data type
 // that holds MAXRECORDS
 // Grades structures.

void readIt(ifstream&, gradeType, const int);

int main()

{    
     ifstream indata;
     indata.open("graderoll.dat");
     int numRecord = 4;                
     gradeType studentRecord; 

     if(!indata)
     {
        cout << "Error opening file. \n";
        cout << "It may not exist where indicated" << endl;
        return 1;
     }

    readIt(indata, studentRecord, MAXRECORDS); 

    // output the information 
        for (int count = 0; count < numRecord; count++)
    {
           cout << studentRecord[count].name << setw(10) 
            << studentRecord[count].test1
            << setw(10) << studentRecord[count].test2;
           cout << setw(10) << studentRecord[count].final << endl;
    }                

    return 0;
}

void readIt(ifstream& inData, gradeType gradeRec, const int max)

{
   int total = 0;

   inData.get(gradeRec[total].name, NAMESIZE);
   while (inData)
   {
     inData >> gradeRec[total].test1;
     inData >> gradeRec[total].test2;
     inData >> gradeRec[total].final;

     total++;    

     inData.clear(); 
     inData.get(gradeRec[total].name, NAMESIZE);


  }

}

Any suggestions to help me out with this?

jww
  • 83,594
  • 69
  • 338
  • 732
jshapy8
  • 1,733
  • 5
  • 23
  • 51

2 Answers2

1

I SOLVED THE ISSUE:

void readIt(ifstream& inData, gradeType gradeRec, const int max)

{
   int total = 0;

   inData.get(gradeRec[total].name, NAMESIZE);
   while (inData)
   {
     inData >> gradeRec[total].test1;
     inData >> gradeRec[total].test2;
     inData >> gradeRec[total].final;

     total++;    

     inData.ignore(NAMESIZE, '\n');
     inData.get(gradeRec[total].name, NAMESIZE);


  }

}

inData.ignore(NAMESIZE, '\n'); will make the character array work properly. This is because the character array is limited to 15 characters. So if specify to ignore the next 15 characters and or until a newline escape sequence is encountered, each line of the file is read properly.

jshapy8
  • 1,733
  • 5
  • 23
  • 51
0

The problem is your use of:

while (inData)

When converting an iostream to a boolean, the boolean reflects the state of the EOF flag. However, the EOF flag is not set until after you try to read data from the end of the file.

So what happens is your code successfully reads the last line of your file, then loops around and the EOF flag is not set yet, and tries to read one more line from the file. This doesn't read any actual data and you get zeros. After that, the EOF flag is set and your loop terminates.

One way to fix this might be to test the EOF flag after reading the expected data, so:

   while (true)
   {
     inData >> gradeRec[total].test1;
     inData >> gradeRec[total].test2;
     inData >> gradeRec[total].final;

     if (!inData) {
       break;
     }

     total++;    

     inData.clear(); 
     inData.get(gradeRec[total].name, NAMESIZE);
   }

You could also rearrange your loop a bit so you don't have code to read the name twice, once outside the loop and once at the end.

Greg Hewgill
  • 828,234
  • 170
  • 1,097
  • 1,237
  • Unfortunately, when I put the condition in the while loop to true, it only reads the first line of the file and puts zeros for the remaining three specified instances, unlike what I had before. It appears the while loop is reading into the array, a line of zeros in between areas in the file. Is there a way where I can tell the program to consume the end of line? – jshapy8 May 03 '15 at 21:51