I have been working on this program for a couple of days now and I still cant seem to get it to do what I want it to do.
I am using fin.peek() to read character from input file and saving each character into an array.
each line from the input has first name, last name, id number, and then 5-6 grades. Random spacing between each one.
input file example:
Adam Zeller 45678 80 87 60 90 88 100
David Young 34521 34 56 76 76 76 76
Carl Wilson 909034 90 90 90 49 39
my code for reading in and storing each character is this:
while(fin.peek() == ' ')
fin.get();
while(fin.peek() != ' ') ///// first name
{
c = fin.get();
first[i] = c;
first[i+1] = '\0';
i++;
}
cout << first << endl;
while(fin.peek() == ' ')
fin.get();
i = 0;
while(fin.peek() != ' ') /////// last name
{
c = fin.get();
last[i] = c;
last[i+1] = '\0';
i++;
}
cout << last << endl;
while(fin.peek() == ' ')
fin.get();
i = 0;
while(fin.peek() != ' ')//////////////////// ID
{
c = fin.get();
iD[i] = c;
iD[i+1] = '\0';
i++;
}
cout << iD << endl;
if(fin.peek() == '-')// Checks for negative number error.
{
cout << "***Error, negative grades not allowed!***";
return -1;
}
if(isdigit(fin.peek()))// record grade
{
fin >> score;
//cout << "inside if loop isdigit: " << score<< endl;
again = true;
}
if(again == true) // place grade in array
{
cout << score << endl;
scoreArray[testNumb] = atof(score);
testNumb++;
}
}
The problem I am having is what sort of loop would go around this to read till end of file. I have tried eof. I have tried while(!fin.peek == \n') and I have tried a couple of other methods. these methods resulted in a never ending loop execution. So now I feel stuck. Any suggestions or hits to lead me in the right directions would be greatly appreciated! Thank you.