I have problem with this code it unable to run, can u help 4 check the problem ?

This is the question:
Write C++ Console Application to calculate and display the average CGPA of 10 students with the following requirements.

Your program must use Class for student which have the following variables/attributes
Name
MatrixNo
Cgpa.
Your program should get information of 10 students Name,MatrixNo,Cgpa) from User and store it in Arrayof object of type Student (from item 1).
Then, your program must calculate and display the average CGPA from the Array(from item 2).

this is my answer :
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

/* Function Declarations */
char calculateGrade(double);
void calcAverage(ifstream &, int [], int, double &);
void calclasseAverage(ifstream &, int [],double &,double &);

/* Entry-Point */

int main(void)

{

string name;

int i,numgrades=5,grade[5];

double average;
double classAvg = 0;
char letter;ifstream inputfile;


inputfile.open("MAYA.txt");

if(inputfile.fail())

{
cout<<"input file did not open please check it\n";
return 1;
}

cout << "Names\t\tTest1\tTest2\tTest3\tTest4\tTest5\tAverage\tGrade\n\n";
inputfile>>name;
while(inputfile)
{
calcAverage(inputfile,grade,numgrades,average);
letter = calculateGrade(average);

cout<<name<<" \t";

for(i=0;i<numgrades;i++)

cout<<grade[i]<<"\t";

cout<<average<<"\t"<<letter<<"\n";

inputfile>>name;
}
calclasseAverage(inputfile,grade,average,classAvg);
cout<<"\nClass Average = "<<classAvg<<endl<<endl;
inputfile.close();


return 0;

}





/* Function to retrieve the letter grade. */

char calculateGrade(double average)

{

if(average >=90)

return 'A';


else if(average >=80)

return 'B';

else if(average >=70)

return 'C';

else if(average>=60)
return 'D';

else

return 'F';

}



/* Function to read an entry and calculate the average. */

void calcAverage(ifstream &in, int grade[], int max, double &average) //

{

int i,sum=0;

for(i=0; i < max ;i++)

{

in>>grade[i];

sum+=grade[i];

}

average=(double)sum/max;
}
/* Function to calculat the class average. */

void calclasseAverage(ifstream &in, int grade[], double &average,double &classAvg) //

{


classAvg = 0;

for(int i = 7 ;i < 8;i++)
for(int j=1; j < 22 ;j++)

{

in>>average;

classAvg = classAvg + average;

}

classAvg = classAvg/20;

}

i don't know what the problem it unable to run
Last edited on
can u post the exact error you are getting ,use code tag to post source code.
I used DEV C++ 4 the answer, i saw its already compile but there is something wrong cus it unable to run
Last edited on
1
2
inputfile.open("MAYA.txt");


does this file exist in the local directory from where you are running the program, or create your own file and give full path example "c:\\temp\\xyz.txt" instead of maya.txt
By looking at your program, your file operations are not standard, and you dont have any check conditions, that is causing your program to crash, google or search in this website how to read a file.
Topic archived. No new replies allowed.