Help: while !eof loop
the loop returns the correct output, but goes one more round then supposed to, and returns an extra line of junk output, how do i correct this?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct A_Student
{
string name;
int midterm;
int final;
int hqgrades; // homework & quiz grades
float hqavg; // homework & quiz average
float avg; // course average
string letter_grade;
} The_Class [15];
/*function HQGRADE*/
float HQGRADE (A_Student arg[])
{
float sum;
float grade;
for (int n=0; n<18; n++)
{
sum = sum + arg[n].hqgrades;
}
grade = sum *100 /180;
return grade;
}
/* swap function */
void swap(A_Student&x, A_Student&y)
{
A_Student temp;
temp = x;
x=y;
y=temp;
}
/*sorting function*/
void selection(A_Student A[], int size)
{
int index;
for ( int pass = 1; pass <=size-1; pass++)
{
index = 0;
for ( int j = 1; j <=size- pass; j++)
{
if (A[j].avg < A[index].avg) index = j;
}
if (index!=size-pass) swap(A[size-pass],A[index]);
}
}
int main ()
{
int x = 0;
int n=0;
int a=0;
int hqsum = 0;
fstream textfile;
textfile.open("sample input.txt");
for (x=0;x<15;x++)
{
while(!textfile.eof())
{
textfile>> The_Class[x].name;
textfile>> The_Class[x].midterm;
textfile>> The_Class[x].final;
while (n<18)
{
textfile >> The_Class[n].hqgrades;
n++;
}
The_Class[x].avg = 0.3*(The_Class[x].midterm + The_Class[x].final)+0.4*HQGRADE(The_Class);
if (The_Class[x].avg >=90) The_Class[x].letter_grade = "A";
else if (The_Class[x].avg >=80) The_Class[x].letter_grade = "B";
else if (The_Class[x].avg >=70) The_Class[x].letter_grade = "C";
else The_Class[x].letter_grade = "F";
//selection(The_Class,15);
cout << The_Class[x].name << " ";
cout << The_Class[x].midterm << " ";
cout << The_Class[x].final << " " ;
cout.precision(2);
cout <<fixed << HQGRADE(The_Class)<<" ";
cout << The_Class[x].avg << " ";
cout<< The_Class[x].letter_grade;
cout<< endl;
n=0;
x++;
}
}
textfile.close();
cout <<endl;
system("pause");
}
|
In order for the eof flag to be set (on) you need to read the eof character from the stream first.
Example file:
This //eof == false, read "This"
is //eof == false, read "is"
a //eof == false, read "a"
test //eof == false, read "test"
file. //eof == false, read "file."
//eof == false, read EOF FLAG
//eof == true, break loop
|
Last edited on
Topic archived. No new replies allowed.