I have a program that is aonly reading the first line of the input from an input file, and i cannot seem to figure out why it wont read the rest 10 lines in the input file. Attched is the code, the input file and the output i get when i ran the code.

#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
#include <cstring>
#include <cstdlib>
using namespace std;



int main()
{
	int stuId,numStud;										// Student Id and Number of students
	double gr1,gr2,gr3,gr4,GPA;									// Grades 1-4
	double tgr1,tgr2,tgr3,tgr4;									// Accumilative totals for Grades 1-4
	double c1avg,c2avg,c3avg,c4avg;								// Course avarage GPAs
	
	numStud = 0;												// initiatizing counter.
	tgr1 = 0;													// Initializing the totals.
	tgr2 = 0;
	tgr3 = 0;
	tgr4 = 0;

	ifstream inFile;
	inFile.open("C:\\bu\\run1.txt");

	if (!inFile)
	{
		cout << " \n\n\t\t*** Empty file ... No data to process *** \n\n\n";
		return 1;
	}

	else													// while(inFile)
	{										
		inFile >>stuId>>gr1>>gr2>>gr3>>gr4;
	
		cout << "\n\t\t Student \t\t GPA \t\t Special Note \n\n";
	}
	
	GPA = (gr1+gr2+gr3+gr4)/4;

	if (GPA >= 3.5)

		cout <<"\t\t  "<<setprecision(4)<<showpoint<<stuId <<"\t\t\t "<<GPA<<"\t\t   "<<"Honors";

	if (GPA < 1.5)
	
		cout <<"\t\t  "<<setprecision(4)<<showpoint<<stuId<<"\t\t\t "<<GPA<<"\t\t   "<<"Warning.\n";
	
	if (GPA <=3.4 && GPA >= 1.5)
	cout <<"\t\t  "<<setprecision(4)<<showpoint<<stuId <<"\t\t\t "<<GPA<<endl;

	numStud ++;
	tgr1 +=gr1;
	tgr2 +=gr2;
	tgr3 +=gr3;
	tgr4 +=gr4;
	
	c1avg = tgr1/numStud;
	c2avg = tgr2/numStud;
	c3avg = tgr3/numStud;
	c4avg = tgr4/numStud;
	
	cout << "\n\n\t\t\t Course 1 average: "<<setprecision(4)<<showpoint<<c1avg<<endl;
	cout << "\t\t\t Course 2 average: "<<setprecision(4)<<showpoint<<c2avg<<endl;
	cout << "\t\t\t Course 3 average: "<<setprecision(4)<<showpoint<<c3avg<<endl;
	cout << "\t\t\t Course 4 average: "<<setprecision(4)<<showpoint<<c4avg<<endl; 
	cout << endl<<endl;

	while (!inFile.eof());

	inFile.close ();
	return 0;
}

Here is the input file contents:

1022 2.0 2.0 2.0 4.0
1319 4.0 0.0 3.6 3.7
1191 3.0 0.0 1.5 1.5
1333 3.6 4.0 2.7 3.2
1032 2.2 3.7 2.6 2.8
1115 1.0 0.0 2.0 1.3
1234 1.0 1.0 1.0 1.0
1551 4.0 4.0 4.0 4.0
1789 4.0 3.1 3.0 2.7
1729 0.0 2.3 2.4 0.0


and here is what i get for output :


Student GPA Special Note

1022 2.500


Course 1 average: 2.000
Course 2 average: 2.000
Course 3 average: 2.000
Course 4 average: 4.000

Any assistance will be greatly appreciated.
Thank you.

In this block of code, you only make one call to 'infile' your data. You should consider infile'ing your data whilst inside of a loop to ensure you will read in data until you reach end of file:

you have this:

else
{	
     //This code to read data only gets called once, therefore only reading one line o' data
     inFile >>stuId>>gr1>>gr2>>gr3>>gr4;
}

To correct this, turn your grade variables into arrays that will store data per each loop iteration:

double gr1[10] ,gr2[10] ,gr3[10] ,gr4[10], GPA[10];
//we could have also just made a single gr[4][10]
					
int i=0;
while(inFile >> stuId[i] >> gr1[i] >> gr2[i] >> gr3[i] >> gr4[i])
{
     i++;	
}

Now you can read in several lines at a time because your loop structure will allow you to extract several lines of data.. until such time data can no longer be extracted (end of file). You also have the appropriate number of storage containers to handle all of the data; into 10 element arrays, one element for each student (one for each line of .txt).

Edited 7 Years Ago by Clinton Portis: n/a

This article has been dead for over six months. Start a new discussion instead.