Hi, can anyone plz check my code to see why in the prinCLASSdata function the int A, int B ... doesn't have a value. even though it is right in the updateFrequencies function.

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

void read_data(int size,string name[],int test1[],int test2[],int test3[],int test4[],int test5[]);
void calculateAverage(int size, string name[],int test1[],int test2[],int test3[],int test4[],int test5[],double average[]);
int calculateGrade(int size,char grade[],double average[]);
void updateFrequency(int size,char grade[],int A,int B,int C,int D,int F);
void printCLASSdata(int size, string name[],int test1[],int test2[],int test3[],int test4[],int test5[],char grade[],double average[],double classAverage, char classGrade,int A,int B,int C,int D,int F);

int main()
{
	const int size=10;
	double classAverage=0;
	int A=0,B=0,C=0,D=0,F=0;
	char classGrade=0;	
	string name[size];
	double average[size];
	char grade[size];
	int test1[size], test2[size], test3[size], test4[size], test5[size];

	read_data(size,name,test1,test2,test3,test4,test5);
	calculateAverage(size,name,test1,test2,test3,test4,test5,average);
	calculateGrade(size,grade,average);
	updateFrequency(size,grade,A,B,C,D,F);
	for(int i=0;i<size;i++)
	{
		classAverage += average[i];
	}
	classAverage /= size;
	if(classAverage>=90)
		classGrade='A';
	else if(classAverage>=80 && classAverage<90)
		classGrade='B';
	else if(classAverage>=70 && classAverage<80)
		classGrade='C';
	else if(classAverage>=60 && classAverage<70)
		classGrade='D';
	else
		classGrade='F';
	printCLASSdata(size,name,test1,test2,test3,test4,test5,grade,average,classAverage,classGrade,A,B,C,D,F);

	system("pause");
	return 0;
}
void read_data(int size,string name[],int test1[],int test2[],int test3[],int test4[],int test5[])
{
	ifstream infile;
	for(int i=0;i<size;i++)
	{
		test1[i]=0;
		test2[i]=0;
		test3[i]=0;
		test4[i]=0;
		test5[i]=0;
	}

	cout<<"Reading from file ''input.txt''..."<<endl;
	infile.open("input.txt");
	if (!infile) 
	{
		cout<< "Unable to open file input.txt"<<endl;
		exit(1);   // call system to stop
	}

	while(!infile.eof())
	{
		for(int i=0;i<size;i++)
		{
			infile>>name[i];
			infile>>test1[i];
			infile>>test2[i];
			infile>>test3[i];
			infile>>test4[i];
			infile>>test5[i];
		}
	}
	
	for(int i=0;i<size;i++)
	{
		cout<<name[i]<<" "<<test1[i]<<" "<<test2[i]<<" "<<test3[i]<<" "<<test4[i]<<" "<<test5[i]<<" "<<endl;
	}
	infile.close();
}
void calculateAverage(int size, string name[],int test1[],int test2[],int test3[],int test4[],int test5[],double average[])
{
	for(int i=0;i<size;i++)
	{
		average[i]=(test1[i]+test2[i]+test3[i]+test4[i]+test5[i])/5;
	}
	for(int i=0;i<size;i++)
	{
		cout<<"The average score for "<<name[i]<<" is: "<<average[i]<<endl;
	}
//	calculateGrade(average);
}

int calculateGrade(int size,char grade[],double average[])
{
	for(int i=0;i<size;i++)
	{
		if(average[i]>=90)
			grade[i]='A';
		else if(average[i]>=80 && average[i]<90)
			grade[i]='B';
		else if(average[i]>=70 && average[i]<80)
			grade[i]='C';
		else if(average[i]>=60 && average[i]<70)
			grade[i]='D';
		else
			grade[i]='F';
		cout<<"The grade is: "<<grade[i]<<endl;
	}
//	updateFrequency(size,grade);
	return 0;
}
void updateFrequency(int size, char grade[],int A,int B,int C,int D,int F)
{
	for(int i=0;i<size;i++)
	{
		if(grade[i]=='A')
			A++;
		else if(grade[i]=='B')
			B++;
		else if(grade[i]=='C')
			C++;
		else if(grade[i]=='D')
			D++;
		else
			F++;
	}
}
void printCLASSdata(int size,string name[],int test1[],int test2[],int test3[],int test4[],int test5[],char grade[],double average[],double classAverage, char classGrade,int A,int B,int C,int D,int F)
{
	cout<<endl;
	cout<<"RESULTS:"<<endl;
	cout<<"Student Test1 Test2 Test3 Test4 Test5 Average Grade"<<endl;
	for(int i=0;i<size;i++)
		cout<<name[i]<<"     "<<test1[i]<<"     "<<test2[i]<<"     "<<test3[i]<<"     "<<test4[i]<<"     "<<test5[i]<<"     "<<average[i]<<"     "<<grade[i]<<endl;
	cout<<endl;
	cout<<"Class Average = "<<classAverage<<endl;
	cout<<"Class Average (as Grade) : "<<classGrade<<endl;
	cout<<endl;
	cout<<"Grade Frequencies:"<<endl;
	cout<<"Grade Frequency"<<endl;
	cout<<"A "<<A<<endl;
	cout<<"B "<<B<<endl;
	cout<<"C "<<C<<endl;
	cout<<"D "<<D<<endl;
	cout<<"F "<<F<<endl;
}

Here is the input.txt data:

Johnson 85 83 77 91 76
Aniston 80 90 95 93 48
Cooper 78 81 11 90 73
Gupta 92 83 30 69 87
Blair 23 45 96 38 59
Clark 60 85 45 39 67
Kennedy 77 31 52 74 83
Bronson 93 94 89 77 97
Sunny 79 85 28 93 82
Smith 85 72 49 75 63

void updateFrequency(int size, char grade[],int A,int B,int C,int D,int F) should have ABCDF passed in by reference as in void updateFrequency(int size, char grade[],int & A,int & B,int & C,int & D,int & F) so that when you are modifying the counts in the function they are changed in main().
Call it as: updateFrequency(size,grade,A,B,C,D,F)

Edited 7 Years Ago by jonsca: n/a

With the help of programmerbook i was able to solve it the problem was that i wasn't passing the updateFrequency variable by reference

wrong:

void updateFrequency(int size,char grade[],int A,int B,int C,int D,int F);
void updateFrequency(int size, char grade[],int A,int B,int C,int D,int F)
{
	for(int i=0;i<size;i++)
	{
		if(grade[i]=='A')
			A++;
		else if(grade[i]=='B')
			B++;
		else if(grade[i]=='C')
			C++;
		else if(grade[i]=='D')
			D++;
		else
			F++;
	}x
}

right:

void updateFrequency(int size,char grade[],int &A,int &B,int &C,int &D,int &F);
void updateFrequency(int size, char grade[],int A,int B,int C,int D,int F)
{
	for(int i=0;i<size;i++)
	{
		if(grade[i]=='A')
			A++;
		else if(grade[i]=='B')
			B++;
		else if(grade[i]=='C')
			C++;
		else if(grade[i]=='D')
			D++;
		else
			F++;
	}
}

Here is the full code:

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

void read_data(int size,string name[],int test1[],int test2[],int test3[],int test4[],int test5[]);
void calculateAverage(int size, string name[],int test1[],int test2[],int test3[],int test4[],int test5[],double average[]);
int calculateGrade(int size,char grade[],double average[]);
void updateFrequency(int size,char grade[],int &A,int &B,int &C,int &D,int &F);
void printCLASSdata(int size, string name[],int test1[],int test2[],int test3[],int test4[],int test5[],char grade[],double average[],double classAverage, char classGrade,int A,int B,int C,int D,int F);

int main()
{
	const int size=10;
	double classAverage=0;
	int A=0,B=0,C=0,D=0,F=0;
	char classGrade=0;	
	string name[size];
	double average[size];
	char grade[size];
	int test1[size], test2[size], test3[size], test4[size], test5[size];

	read_data(size,name,test1,test2,test3,test4,test5);
	calculateAverage(size,name,test1,test2,test3,test4,test5,average);
	calculateGrade(size,grade,average);
	updateFrequency(size,grade,A,B,C,D,F);
	for(int i=0;i<size;i++)
	{
		classAverage += average[i];
	}
	classAverage /= size;
	if(classAverage>=90)
		classGrade='A';
	else if(classAverage>=80 && classAverage<90)
		classGrade='B';
	else if(classAverage>=70 && classAverage<80)
		classGrade='C';
	else if(classAverage>=60 && classAverage<70)
		classGrade='D';
	else
		classGrade='F';
	printCLASSdata(size,name,test1,test2,test3,test4,test5,grade,average,classAverage,classGrade,A,B,C,D,F);

	system("pause");
	return 0;
}
void read_data(int size,string name[],int test1[],int test2[],int test3[],int test4[],int test5[])
{
	ifstream infile;
	for(int i=0;i<size;i++)
	{
		test1[i]=0;
		test2[i]=0;
		test3[i]=0;
		test4[i]=0;
		test5[i]=0;
	}

	cout<<"Reading from file ''input.txt''..."<<endl;
	infile.open("input.txt");
	if (!infile) 
	{
		cout<< "Unable to open file input.txt"<<endl;
		exit(1);   // call system to stop
	}

	while(!infile.eof())
	{
		for(int i=0;i<size;i++)
		{
			infile>>name[i];
			infile>>test1[i];
			infile>>test2[i];
			infile>>test3[i];
			infile>>test4[i];
			infile>>test5[i];
		}
	}
	
	for(int i=0;i<size;i++)
	{
		cout<<name[i]<<" "<<test1[i]<<" "<<test2[i]<<" "<<test3[i]<<" "<<test4[i]<<" "<<test5[i]<<" "<<endl;
	}
	infile.close();
}
void calculateAverage(int size, string name[],int test1[],int test2[],int test3[],int test4[],int test5[],double average[])
{
	for(int i=0;i<size;i++)
	{
		average[i]=(double)(test1[i]+test2[i]+test3[i]+test4[i]+test5[i])/5;
	}
	for(int i=0;i<size;i++)
	{
		cout<<"The average score for "<<name[i]<<" is: "<<average[i]<<endl;
	}
//	calculateGrade(average);
}

int calculateGrade(int size,char grade[],double average[])
{
	for(int i=0;i<size;i++)
	{
		if(average[i]>=90)
			grade[i]='A';
		else if(average[i]>=80 && average[i]<90)
			grade[i]='B';
		else if(average[i]>=70 && average[i]<80)
			grade[i]='C';
		else if(average[i]>=60 && average[i]<70)
			grade[i]='D';
		else
			grade[i]='F';
		cout<<"The grade is: "<<grade[i]<<endl;
	}
//	updateFrequency(size,grade);
	return 0;
}
void updateFrequency(int size, char grade[],int &A,int &B,int &C,int &D,int &F)
{
	for(int i=0;i<size;i++)
	{
		if(grade[i]=='A')
			A++;
		else if(grade[i]=='B')
			B++;
		else if(grade[i]=='C')
			C++;
		else if(grade[i]=='D')
			D++;
		else
			F++;
	}
}
void printCLASSdata(int size,string name[],int test1[],int test2[],int test3[],int test4[],int test5[],char grade[],double average[],double classAverage, char classGrade,int A,int B,int C,int D,int F)
{
	cout<<endl;
	cout<<"RESULTS:"<<endl;
	cout<<"Student Test1 Test2 Test3 Test4 Test5 Average Grade"<<endl;
	for(int i=0;i<size;i++)
		cout<<name[i]<<"     "<<test1[i]<<"     "<<test2[i]<<"     "<<test3[i]<<"     "<<test4[i]<<"     "<<test5[i]<<"     "<<average[i]<<"     "<<grade[i]<<endl;
	cout<<endl;
	cout<<"Class Average = "<<classAverage<<endl;
	cout<<"Class Average (as Grade) : "<<classGrade<<endl;
	cout<<endl;
	cout<<"Grade Frequencies:"<<endl;
	cout<<"Grade Frequency"<<endl;
	cout<<"A "<<A<<endl;
	cout<<"B "<<B<<endl;
	cout<<"C "<<C<<endl;
	cout<<"D "<<D<<endl;
	cout<<"F "<<F<<endl;
}
This question has already been answered. Start a new discussion instead.