I don't understand where the last line of my input is coming from....The program should stop reading with Hebb,Emily, but it seems that it's trying to bring in another line of information...any ideas?

Total Number of Students: 73
 ID      LName      FName          Q1   Q2   Q3   MT   Q4   Q5   Q6    F   Avg  Grade
-------------------------------------------------------------------------------------
45256   Rodrigues   Joana          58   75   58   61   59   75   63   92  66.2    Y
37915   Wright      Michelle       98   83   56   62   63   90   57   67  62.6    Y
81984   Williams    Jenny          55   67   54   63   89   84   93   75  66.0    Y
73984   Phaneuf     Lesley         78   85   57   51   68   94   51   83  65.1    Y
80886   Laflamme    Nicole         76   51   71   94   69   78   87   91  74.3    Y
39473   Kenyon      Patricia       65   54   90   68   94   70   95   97  75.5    Y
12127   McCabe      Kelly          51   96    0   64   54   75   71   94  60.1    Y
52458   Whitten     Sarah          99   58   94   82   81   75   82   70  69.5    Y
77921   Connors     Sarah          52   58   88   63   61   65   78   78  64.5    Y
28810   Navin       Joshua         94   75   62   93   64   92   87   94  78.8    Y
31571   Toporowski  Crystal        93    0   77   77   63   68   88   58  56.7    Y
33580   Ziolko      John           74   64   98   92   98   89    0   79  67.8    Y
14508   Stronach    Kurt           80   95   96   84   78   86   53   59  68.6    Y
44520   Ecklord     Ryan            0   61   56   86   98   98   59   83  72.1    Y
15246   Berling     Danielle       85   64    0   75   69    0   54   85  55.7    Y
16137   Littlefield Arionna        71   85   74   97   69   64   82   95  75.2    Y
62631   Niedojadlo  Evan           73   66   83   97   97   51   66   88  72.5    Y
59640   Knieriem    Brandon        52   87   97   66   97   90   93   56  69.5    Y
44102   Spence      Arthur         85   84  100   82   91    0   95   62  65.7    Y
42331   Rose        Nicole         83   50   96   68   54   62    0   93  64.2    Y
72054   Houde       Jessica        72   66   71   62   75   71   86  100  78.6    Y
24609   Cooper      Camille        62   93   92   72   58   76   57   66  65.6    Y
85736   Hepburn     Spencer        71    0   62   60   52   62   64   51  46.6    Y
87129   Morang      Nicholas       77   67   57   89   88   64   68   78  69.6    Y
43865   Hildebrandt Stephenson     97   94   93   93   82   88   78  100  85.1    Y
78575   Suslovic    Vikilynn       65   54   74   67   61   76   69   76  63.3    Y
58751   Flores      Jose           58   80    0   74   87   95   96   69  62.8    Y
18140   Galotti     Salvator       78   65   90   66   88   57   93   98  75.5    Y
10364   Denaro      Tony           73   85   55   72   68   91   55   89  68.5    Y
99189   Vasquez     Oskar          66   60    0   75   63   92   92   52  56.3    Y
21705   Roy         Jake           86    0   89   94   82    0   60   98  64.8    Y
50838   Burr        Jermiah        83   84   94   90   77   73   72   63  77.5    Y
32998   Benway      Eileen         76   98   80   69   75   94   77   95  76.6    Y
87361   Perlmutter  Diadre         96   60   81   88   81   53   91   74  70.2    Y
61084   Cruz        Waldemar       70   68   76   87  100   80   92   86  77.1    Y
54378   Franco-BanksNixaliz        99   58   80   66   63   59   98   83  67.2    Y
10984   Juste       Kerlande       97   73   98   86   96   73   68   57  77.1    Y
77463   Weber       Jessica        60   56   77   62   81   74   78   69  63.4    Y
36278   Hartman     Kristine      100   62   71  100   69   51   58   55  61.5    Y
94304   Bowdy       James           0   57   70  100   92   84   50   98  77.0    Y
55937   Kantzios    Angela         55    0   78   86   93   73   82   76  66.0    Y
17600   DeNinno     David          62   68   93   88   84   60   98   75  70.7    Y
28248   Courville   David          81   88   89   52   64   79   89   87  69.6    Y
61316   Eaton       John           80   51   93   67   90   92   76   69  67.5    Y
84511   Calluari    Henry          72   84   97   59   73    0   86   74  63.0    Y
39234   Spaulding   Nicholas       61    0   76   90   92   53   75   54  58.0    Y
33068   King        Nicholas       70   83   83   89   51   98   75   70  69.5    Y
45403   Breault     Matthew        52   75   70   64   91   59   57   94  69.3    Y
29303   Morales     Lynaska        65   56   97   65   98   73   99   76  71.0    Y
28432   Calle       Juan          100   51   51   65   69   59   68   90  63.5    Y
85434   Hagen       Amanda         72   90   99   71   87    0   81   98  72.2    Y
79998   Szostak     Patrycja       58   67   56   68   77   96    0   81  59.6    Y
19374   Gurung      Manoj          98   85   92   93   85   95   93   99  84.8    Y
46103   Shaw        Lindsay        97   85   85   89   64   57   89   73  71.4    Y
13009   Palko       Michael        98   82   92   51    0   80   73   88  63.5    Y
55698   Reyes       Joselina       59   81   53   90   80   94   79   93  74.9    Y
64779   Alba        Sasha          55   67   93   64    0   91   92   55  54.4    Y
86071   Aimetti     Sean           51   83   88   98   91   83   75   66  74.0    Y
33205   Roberts     Thomas         86   94   70   88   64   88   64   87  75.7    Y
81233   Procko      Nancy          69   51   57   80   52   66   61   51  54.5    Y
90219   June        Christopher    66   86   80   60   71    0   76   80  61.7    Y
39285   Calluari    Joel           77   57   71   78   58   94   71   75  66.5    Y
44686   Soulliere   Jane           69   55   80   95   67   76   89   62  67.5    Y
51785   Kokoszka    Elizabeth      71   61   77   83    0   96   84   56  58.7    Y
26789   Irizarry    Elsie          87   77   76   68   65  100   95   79  72.5    Y
11371   Berry       Angela         90   77   54   76   90   64   97   77  70.7    Y
92521   Cyr         Gregory        59   86   97   85   59   62    0   72  62.7    Y
90742   Manfredi    Kathryn        94   65   81   90   67   66   79   52  71.6    Y
71831   Oquendo     Jessica        60   60   57   59   53   56   78   57  54.2    Y
19991   Lucas       Trever          0   77   90   75   53   88   61   63  64.4    Y
63757   Jones       James          82   67   59   85   80   89   93   87  75.8    Y
78759   Komorowski  Ethan          69   69   79   82   61   63   82   55  68.4    Y
99076   Hebb        Emily          78   68   59   77    0   93   68   54  56.6    Y
-858993460                           17681522261408    0-859045684    0-858993664    01743532888  0.0    ☺

                       Average:  67.7 66.9 73.8 77.1 71.0 70.7 73.2 76.5-858993460.0

Press any key to continue . . .
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;

struct student
{
	int id;
	string fname;
	string lname;
	int gr[8];
	double avg;
	char letGr;
};

void print(student arr[], int cnt)
{
	cout << endl;
	cout << "Total Number of Students: " << cnt;
	cout << endl;
	cout << " ID      LName      FName          Q1   Q2   Q3   MT   Q4   Q5   Q6    F   Avg  Grade" << endl;
	cout << "-------------------------------------------------------------------------------------" << endl;
	for (int i=0;i<=cnt;i++)
	{
		cout << setw(2) << arr[i].id << "   " << left << setw(12) << arr[i].fname 
			 << setw(12) << arr[i].lname << right;
		for (int j=0;j < 8;j++)
			cout << setw(5) << arr[i].gr[j];
		cout << "  " << arr[i].avg  << "    " << arr[i].letGr << endl;
	}
	cout << endl;
}
double studAvg(student rec)
{
	int s=0;
	int m=0;
	int f=0;

	for (int i=0; i < 8; i++)
		s += rec.gr[i];
	m = rec.gr[3];
	f = rec.gr[7];

	if (rec.gr[0] <= rec.gr[1] && rec.gr[0] <= rec.gr[2] && rec.gr[0] <= rec.gr[3] && rec.gr[0] <= rec.gr[4] && rec.gr[0] 
		<= rec.gr[5] && rec.gr[0] <= rec.gr[6] && rec.gr[0] <= rec.gr[7])
		return (((s - rec.gr[0]) + m + (2 * f)) / 11.0);
	else if (rec.gr[1] <= rec.gr[0] && rec.gr[1] <= rec.gr[2] && rec.gr[1] <= rec.gr[3] && rec.gr[1] <= rec.gr[4] && rec.gr[1] 
		<= rec.gr[5] && rec.gr[1] <= rec.gr[6] && rec.gr[1] <= rec.gr[7])
		return (((s - rec.gr[0]) + m + (2 * f)) / 11.0);
	else if (rec.gr[2] <= rec.gr[0] && rec.gr[2] <= rec.gr[1] && rec.gr[2] <= rec.gr[3] && rec.gr[2] <= rec.gr[4] && rec.gr[2] 
		<= rec.gr[5] && rec.gr[2] <= rec.gr[6] && rec.gr[2] <= rec.gr[7])
		return (((s - rec.gr[1]) + m + (2 * f)) / 11.0);
	else if (rec.gr[4] <= rec.gr[0] && rec.gr[4] <= rec.gr[2] && rec.gr[4] <= rec.gr[1] && rec.gr[4] <= rec.gr[4] && rec.gr[4] 
		<= rec.gr[5] && rec.gr[4] <= rec.gr[6] && rec.gr[4] <= rec.gr[7])
		return (((s - rec.gr[2]) + m + (2 * f)) / 11.0);
	else if (rec.gr[5] <= rec.gr[0] && rec.gr[5] <= rec.gr[1] && rec.gr[5] <= rec.gr[2] && rec.gr[5] <= rec.gr[3] && rec.gr[5] 
		<= rec.gr[4] && rec.gr[5] <= rec.gr[6] && rec.gr[5] <= rec.gr[7])
		return (((s - rec.gr[3]) + m + (2 * f)) / 11.0);
	else if (rec.gr[6] <= rec.gr[0] && rec.gr[6] <= rec.gr[1] && rec.gr[6] <= rec.gr[2] && rec.gr[6] <= rec.gr[3] && rec.gr[6] 
		<= rec.gr[4] && rec.gr[6] <= rec.gr[6] && rec.gr[6] <= rec.gr[7])
		return (((s - rec.gr[4]) + m + (2 * f)) / 11.0);
	else
		return ((s + m + (2 * f)) / 11.0);
}

double average(student arr[], int cnt, int i)
{
	double s=0;
		
	for (int j=0; j < cnt; j++)
		if (i > 0)
			s += arr[j].gr[i];
		else
			s += arr[j].avg;
	
	return s/cnt;
}

char letterGrade(double sAvg, double cAvg)
{
	if (sAvg >= cAvg + 15) return 'A';
	else if (sAvg >= cAvg + 5) return 'B';
	else if (sAvg >= cAvg - 5) return 'C';
	else if (sAvg >= cAvg -15) return 'D';
	else return 'F';
}

void sort(student arr[], int c)
{
	student t;

	for (int i=c-1; i < 1; i--)
		for (int j=1; j < i; j++)
			if (arr[j+1].fname < arr[j].fname)
			{
				t = arr[j]; arr[j] = arr[j+1]; arr[j+1] = t;
			}
}


void main()
{
	ifstream inF;
	student myStuds[73];
	int cnt=0;
	int i;
	double classAvg;
	string fname, lname, id;

	cout << fixed << setprecision(1);

	inF.open("students.txt");


	inF >> myStuds[cnt].id;
	while (!inF.eof())
	{
		inF >> myStuds[cnt].fname;
		inF >> myStuds[cnt].lname;
		for (i = 0; i < 8; i++)
			inF >> myStuds[cnt].gr[i];
		myStuds[cnt].avg = studAvg(myStuds[cnt]);
		myStuds[cnt].letGr = 'Y';

		cnt++;
		inF >> myStuds[cnt].id;
	}
	inF.close();
	classAvg = average(myStuds, cnt, i);
	print(myStuds, cnt);
	cout << "                       Average: ";
	for (int i=0; i < 8; i++)
		cout << setw(5) << average(myStuds, cnt, i);
	cout << setw(6) << classAvg << endl << endl;
}

You're iterating too far.

The element indexed by cnt wasn't fully read.

On Line 25 the loop limit should be i < cnt not i <= cnt

You're iterating too far.

The element indexed by cnt wasn't fully read.

On Line 25 the loop limit should be i < cnt not i <= cnt

Thanks!

Frist, main() should return type int, not type void. Even if your compiler allows you do use reurn type void, don't do it.

The problem you allude to however, probably has to do with line 118. If you use the return value of eof() to control performance of the loop body you are likely to run into errors of over reading by one because of the way eof() works. I find it confusing to explain why this is so and I haven't seen any simple, straightforward explanations I can link to. So, for now, don't do it. To fix the problem I would try combining lines 117 and line 118 into this:

while(inF >> myStuds[cnt].id)

and eliminat lines 117 and line 128.

It turns out that when stream input methods fail to read into variables for whatever reason the return value will equate to false and the loop will stop without doing the loop body. To find out why the loop stopped you can then inquire why using eof(). If the loop stopped because it found the end of file (ie, all the file was read in without problem) that is good news. If the loop didn't stop because it found the end of file, then input stopped for some other, unknown reason, that you may or may not be able to track down, fix, and be able to continue. For example, if the file is corrupt for any reason, then input may stop for reasons other than finding end of file.

EDIT: I like Murtan's answer better than mine, but would encourage you to not use eof() in the context you did, nevertheless, because even if it wasn't the cause of the problem this time, it probably will be at some point in the future.

Edited 6 Years Ago by Lerner: n/a

This question has already been answered. Start a new discussion instead.