Hi, sorry I'm posting so much all off a sudden, but I recently found these forums, and they've been a lot of help.
I'm have to read into the program from a file and output into another file. The problem is in function calculateGrade. When I output, I get a crazy symbol. I know the problem is in the loop. Can anybody help?
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
const int noOfStudent=20;
struct studentType
{
string studentFName;
string studentLName;
int testScore;
char grade;
};
void getData (ifstream& inFile, studentType sList[], int listSize);
void calculateGrade (ifstream& inFile, studentType sList[], int listSize);
int highestScore (const studentType sList[], int listSize);
void printResult (ofstream& outFile, const studentType sList[], int listSize);
int main()
{
ifstream inData;
ofstream outData;
studentType studentList[noOfStudent];
inData.open("F:\\Ch11_Ex01Data.txt");
if (!inData)
{
cout<<"The input file does not exist. Program terminates!"<<endl;
return 1;
}
outData.open("F:\\Ch11_Ex01out.txt");
if (!outData)
{
cout<<"Cannot open the output file. Program terminates!"<<endl;
return 1;
}
getData(inData, studentList, noOfStudent);
calculateGrade(inData, studentList, noOfStudent);
printResult(outData, studentList, noOfStudent);
return 0;
}
void getData(ifstream& inFile, studentType sList[], int listSize)
{
string studentFName;
string studentLName;
int testScore;
int i;
for (i=0; i<listSize; i++)
{
inFile>>sList[i].studentFName>>sList[i].studentLName>>sList[i].testScore;
}
}
void calculateGrade(ifstream& inFile, studentType sList[], int listSize)
{
char grade;
int testScore, i;
for (i=0; i<listSize; i++)
{
inFile>>sList[i].testScore;
if (testScore<=100 && testScore>=90)
{
grade = 'A';
}
else if (testScore<=89 && testScore>=80)
{
grade = 'B';
}
else if (testScore<=79 && testScore>=70)
{
grade = 'C';
}
else if (testScore<=69 && testScore>=60)
{
grade = 'D';
}
else
{
grade = 'F';
}
}
}
int highestScore(const studentType sList[], int listSize)
{
int hScore=sList[0].testScore;
return hScore;
}
void printResult(ofstream& outFile, const studentType sList[], int listSize)
{
string studentFName;
string studentLName;
int maxScore= highestScore(sList, listSize);
int i, testScore;
char grade;
outFile << setw(15) << "Student Name "
<< setw(14) << "Test Score"
<< setw(7) << "Grade" << endl<<endl;
for (i=0; i<listSize; i++)
{
outFile<<setw(10)<<left<<sList[i].studentLName<<left<<", "<<setw(15)<<left<<sList[i].studentFName
<<" "<<setw(10)<<left<<maxScore<<setw(7)<<grade<<endl;
}
}