#include <fstream> #include <iostream> #include <iomanip> using namespace std; /
ID: 3868752 • Letter: #
Question
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
// Declaring global arrays and variables
string lastnames[40];
int scores[40][5];
double avgArr[40];
char grades[40];
int noOfStuds;
// Function Declarations
void readData(ifstream& dataIn);
char calGrade(double average);
void displayData(ofstream& dataOut);
int main()
{
ifstream dataIn;
ofstream dataOut;
readData(dataIn);
displayData(dataOut);
return 0;
}
void readData(ifstream& dataIn)
{
string name;
int score, i = 0;
char grade;
double avg = 0.0, total;
cout << setprecision(1) << fixed << showpoint;
dataIn.open("scores.txt");
if (dataIn.fail())
{
cout << "I can't find the file";
exit(1);
}
else
{
while (!dataIn.eof())
{
total = 0.0;
dataIn >> name;
lastnames[i] = name;
for (int j = 0; j < 5; j++)
{
dataIn >> score;
scores[i][j] = score;
total += score;
}
avg = total / 5;
avgArr[i] = avg;
grade = calGrade(avg);
grades[i] = grade;
i++;
noOfStuds++;
}
dataIn.close();
}
}
/* function implementation which will
* calcualte the grade based on the average
*/
char calGrade(double average)
{
char grade;
if (average >= 90 && average <= 100)
grade = 'A';
else if (average >= 80 && average <= 89)
grade = 'B';
else if (average >= 70 && average <= 79)
grade = 'C';
else if (average >= 60 && average <= 69)
grade = 'D';
else if (average >= 0 && average <= 59)
grade = 'F';
return grade;
}
void displayData(ofstream& dataOut)
{
dataOut.open("grades.txt");
cout << "Last Name Score1 Score2 Score3 Score4 Score5 Avg Grade" << endl;
cout << "--------- ------ ------ ------ ------ ------ --- -----" << endl;
dataOut << "Last Name Score1 Score2 Score3 Score4 Score5 Avg Grade" << endl;
dataOut << "--------- ------ ------ ------ ------ ------ --- -----" << endl;
for (int i = 0; i < noOfStuds; i++)
{
cout << setw(10) << lastnames[i] << " ";
dataOut<< setw(10) << lastnames[i] << " ";
for (int j = 0; j < 5; j++)
{
cout << scores[i][j] << " ";
dataOut << scores[i][j] << " ";
}
cout << avgArr[i] << " " << grades[i] << endl;
dataOut << avgArr[i] << " " << grades[i] << endl;
}
dataOut.close();
}
What is problem of this?
Explanation / Answer
The problem with the program.
for (int j = 0; j < 5; j++)
{
dataIn >> score;
scores[i][j] = score;
total += score;
}
from this piece of code it is not clear as which score to read from file.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.