Processing Data From a File

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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;
// Function prototypes
void avgGrd(int, int, int, int, int, double&, string&);
int findLow(int, int, int, int);
int findHigh(int, int, int, int);

void main()
{
	// Variable declarations
	ifstream inFile;
	string lastName, firstName, grade, file;
	int ID, q1, q2, q3, q4, final, count = 0;
	int sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0, sumFinal = 0, sumAvg = 0;
	int q1Count = 0, q2Count = 0, q3Count = 0, q4Count = 0, finalCount = 0, avgCount = 0;
	int q1Average, q2Average, q3Average, q4Average, finalAverage, avgAverage;
	double average;

	
	cout << "Enter file name: "; cin >> file;
	inFile.open(file + ".txt");
	if (!inFile.fail())
	{
		cout << fixed << setprecision(1);
		cout << endl << setw(53[code]) << "Fall 2012 Student Grade Report:" << endl << endl;

cout << "No. ID Last Name First Name Quiz1 Quiz2 Quiz3 Quiz4 Final Avg Grd" << endl;
cout << "------------------------------------------------------------------------------" << endl;
inFile >> ID;
while (!inFile.eof())
{

count++;
inFile >> lastName >> firstName >> q1 >> q2 >> q3 >> q4 >> final;
sum1 += q1; sum2 += q2; sum3 += q3; sum4 += q4; sumFinal += final;
avgGrd(q1, q2, q3, q4, final, average, grade);
sumAvg += average;
cout << right << setw(3) << count << " " << setw(6) << ID << " " << left
<< setw(12) << lastName << " " << left << setw(12) << firstName << " "<< setw(6) << q1
<< setw(6) << q2 << setw(6) << q3 << setw(6) << q4
<< setw(6) << final << setw(6) << average << setw(5) << grade << endl;
inFile >> ID;
}
inFile.close();
cout << " --------------------------------------------" << endl;
q1Average = (sum1 / count); q2Average = (sum2 / count); q3Average = (sum3 / count);
q4Average = (sum4 / count); finalAverage = (sumFinal / count); avgAverage = (sumAvg / count);
cout << setw(39) << q1Average << setw(6) << q2Average << setw(6) << q3Average << setw(6)
<< q4Average << setw(6) << finalAverage << setw(6) << avgAverage << endl;

}
else
cout << endl << "File Open Failed!" << endl;
}

// Function definitions
void avgGrd(int q1, int q2, int q3, int q4, int final, double& average, string& grade)
{
int low = findLow(q1, q2, q3, q4);
int high = findHigh(q1, q2, q3, q4);
average = ((q1 + q2 + q3 + q4 + final + final) - low + high) / 6.;
if (average >= 96.66)
grade = "A+";
else if (average >= 93.33)
grade = 'A';
else if (average >= 90)
grade = "A-";
else if (average >= 86.66)
grade = "B+";
else if (average >= 83.33)
grade = 'B';
else if (average >= 80)
grade = "B-";
else if (average >= 76.66)
grade = "C+";
else if (average >= 73.33)
grade = 'C';
else if (average >= 70)
grade = "C-";
else if (average >= 66.66)
grade = "D+";
else if (average >= 63.33)
grade = 'D';
else if (average >= 60)
grade = "D-";
else (average < 60);
grade = 'F';

}
int findLow(int q1, int q2, int q3, int q4)
{
int low = q1;
if (q2 < low)
low = q2;
if (q3 < low)
low = q3;
if (q4 < low)
low = q4;
return low;
}
int findHigh(int q1, int q2, int q3, int q4)
{
int high = q1;
if (q2 > high)
high = q2;
if (q3 > high)
high = q3;
if (q4 > high)
high = q4;
return high;
}[/code]
Okay so for this code I have to have it all output in a specific way which it does. The first problem I'm having is that every grade comes up as an 'F' and I'm not sure why. Secondly I am supposed to ignore the zeros and replace them with a blank space. Seeing as how I can't figure out how to do that the averages i need are not right. Can anyone PLEASE HELP me?!
Last edited on
And I also can't figure out how to make it all appear as code.
1
2
else (average < 60);
grade = 'F';


Might have something to do with that stray semi-colon after the else.
I did fix that and it works now. Thank-you.
I also have to write and call a function that when the student id is inputted the First and last name as well as the avg and letter grade are outputted. I have no idea where to start. any suggestions?
cout << "Enter Student ID (999 to end): "; cin >> searchId;
inFile.open(file + ".txt");
inFile.ignore(80, '\n');
while (searchId != 999 && searchId != ID)
{
for (int i = 1; i <= count; i++)
{
inFile >> ID >> lastName >> firstName >> q1 >> q2 >> q3 >> q4 >> final;
if (searchId == ID)
{
avgGrd(q1, q2, q3, q4, final, average, grade);
cout << " " << left << setw(12) << lastName << " " << setw(12) << firstName << ' '
<< setw(3) << average << ' ' << setw(2) << grade << endl;
cout << "Enter Student ID (999 to end): "; cin >> searchId;
}
}
} cout << "Enter any character: "; cin >> end;
This is what I have so far and the only problem I'm having is what to do with numbers that are entered that don't match any ID's
Topic archived. No new replies allowed.