0

I am trying to add a row of int's that are located in separate arrays within my code. I am trying to get these grades to add and then turn it into an avg. My output is crazy though, and I can't figure out why....any ideas?

this is the information in the file:

45256	Rodrigues	Joana		58	75	58	61	59	75	63	92
37915	Wright		Michelle	98	83	56	62	63	90	57	67
81984	Williams	Jenny		55	67	54	63	89	84	93	75
73984	Phaneuf		Lesley		78	85	57	51	68	94	51	83
80886	Laflamme	Nicole		76	51	71	94	69	78	87	91
Enter name of file
students

StudentID    LastName    FirstName   Q1     Q2     Q3     Mid    Q4      Q5     Q6     Final    Avg.    Grade
--------------------------------------------------------------------------------------------
45256        Rodrigues   Joana       58     75     58     61     59      75     63      92      72
72
70
77
79
63
80
67
82
65
74
78

44520        Ecklord     Ryan        0      61     56     86     98      98     59      83      54
79
77
79
74
63
75
72
52
73
90
67


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

void CalculateAvg (ifstream& students, string studentid[60], string fname[60], string lname[60],
					int Q1[60], int Q2[60], int Q3[60], int MidTerm[60], int Q4[60], 
					int Q5[60], int Q6[60], int Final[60]);

void main()
{
	ifstream students;
	ofstream out;

	string fname[60];
	string studentid[60];
	string lname[60];
	string id[60];
	int Q1[60];
	int Q2[60];
	int Q3[60];
	int MidTerm[60];
	int Q4[60];
	int Q5[60];
	int Q6[60];
	int Final[60];

	string file;

	int i;

	cout << "Enter name of file" << endl;
	cin >> file; file += ".txt";

	students.open(file.c_str());
	if(students)
	{
		out.open("CompleteReport.txt");
		out << "Student Grade Report:" << endl;
		cout << fixed << setprecision(1);
		out << fixed << setprecision(1);
		cout << endl 
			<< "StudentID    LastName    FirstName   Q1     Q2     Q3     Mid    Q4      Q5     Q6     Final    Avg.    Grade" 
			<< endl;
		cout << "--------------------------------------------------------------------------------------------"
			<<endl;
		out << "StudentID    LastName    FirstName   Q1     Q2     Q3     Mid    Q4      Q5     Q6     Final    Avg.    Grade" 
			<< endl;
		out << "--------------------------------------------------------------------------------------------"
			<<endl;

		for (i = 0; i < 6; ++i )
		{
			if ( students >> studentid[i] >> fname[i] >> lname[i] >> Q1[i] >> Q2[i] >> Q3[i] >> MidTerm[i] 
					>> Q4[i] >> Q5[i] >> Q6[i] >> Final[i])
			{
				cout << left << setw(13) << studentid[i] << setw(12) << fname[i] << setw(12) << lname[i] << setw(7)
					<< Q1[i] << setw(7) << Q2[i] << setw(7) << Q3[i] << setw(7) << MidTerm[i] << setw(8)
					<< Q4[i] << setw(7) << Q5[i] << setw(8) << Q6[i] << setw(8) << Final[i];
					CalculateAvg (students, id, fname, lname, Q1, Q2, Q3, MidTerm, Q4, Q5, Q6, Final);
				out << left << setw(13) << studentid[i] << setw(12) << fname[i] << setw(8) << lname[i] << setw(7)
					<< Q1[i] << setw(7) << Q2[i] << setw(7) << Q3[i] << setw(7) << MidTerm[i] << setw(8)
					<< Q4[i] << setw(7) << Q5[i] << setw(8) << Q6[i] << setw(8) << Final[i];
					CalculateAvg (students, id, fname, lname, Q1, Q2, Q3, MidTerm, Q4, Q5, Q6, Final);

				cout << endl;
			}
			else
			{
				break;
			}
		}
		students.close();
	}
}


void CalculateAvg (ifstream& students, string studentid[60], string fname[60], string lname[60],
					int Q1[60], int Q2[60], int Q3[60], int MidTerm[60], int Q4[60], 
					int Q5[60], int Q6[60], int Final[60])
{
	int i;
	
	for (i=0; i < 6; i++)
	{
		if ( students >> studentid[i] >> fname[i] >> lname[i] >> Q1[i] >> Q2[i] >> Q3[i] >> MidTerm[i] 
					>> Q4[i] >> Q5[i] >> Q6[i] >> Final[i])
			{
				cout << (Q1[i] + Q2[i] + Q3[i] + MidTerm[i] + Q4[i] + Q5[i] + Q6[i] + Final[i]) / 8;
				cout << endl;
			}
	}
}

Edited by PDB1982: n/a

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by jonsca
1

Why not change calculateAverage to return a double (so your average isn't truncated) and call the function in place from your "out" processing.

double CalculateAvg (ifstream& students, string studentid[60], string fname[60], string lname[60], int Q1[60], int Q2[60], int Q3[60], int MidTerm[60], int Q4[60], int Q5[60], int Q6[60], int Final[60],int i)
{
    return ((Q1[i] + Q2[i] + Q3[i] + MidTerm[i] + Q4[i] + Q5[i] + Q6[i] + Final[i]) / 8);
   //scratch my earlier response, you want to pass in i to get which student to average
// and the fact that you were reading in stuff in this function was "using up" lines of your file		
}

and

out << left << setw(13) << studentid[i] << setw(12) << fname[i] << setw(8) << lname[i] << setw(7)<< Q1[i] << setw(7) << Q2[i] << setw(7) << Q3[i] << setw(7) << MidTerm[i] << setw(8)
<< Q4[i] << setw(7) << Q5[i] << setw(8) << Q6[i] << setw(8) << Final[i]<<CalculateAvg (students, id, fname, lname, Q1, Q2, Q3, MidTerm, Q4, Q5, Q6, Final,i);

That way you have the calculation the way you want it and can format the return value in whatever way you want.

Edited by jonsca: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.