Hi. I have this program I'm writing that I'm stuck on. The first part needs to read in a list of student ID's and a corresponding test score (example input file below). Scores are int values between 1-10. A student that did not take the test will have a -1 score and should not be counted towards the average.
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <string>
usingnamespace std;
int main()
{
ifstream inputFile;
int score, avg, numofstudents = 0, totalgrade = 0;
string id, grade;
inputFile.open("TestScores.txt");
//get the average
while (!inputFile.eof())
{
inputFile >> id;
cout << left << setw(10) << id;
inputFile >> score;
cout << left << setw(10) << score << endl;
if (score >= 0 && score <= 10)
{
totalgrade = totalgrade + score;
numofstudents++;
}
}
avg = totalgrade/numofstudents;
cout << "The avg score is " << avg << endl << endl;
Where I'm stuck is the next part of the program. I need to create a new output file that has a new column with the converted letter grade. So I need to take the average from the first part above and generate the letter grade. (-1 gets no grade, average or the average + 1 = B, average + 2 or average + 3 = A-, +4 ( or 10 if average is 7 or more ) = A, average -1 = C and average – 2 or less = F.)
Needed output file:
1234 1 F
3456 3 C
3453 5 B
4365 9 A
3456 -1
I started with this... but then I stopped short cause I don't think a switch is the way to go. I can't figure out the best way to go about this. Any suggestions?
1 2 3 4 5 6 7 8 9 10 11
inputFile.open("TestScores.txt");
while (!inputFile.eof())
{
inputFile >> id;
inputFile >> score;
if (score >= 0 && score <= 10)
{
switch(score)
case
You can't compute the letter grade inside the loop because you need the average grade to compute them, and you don't have it yet.
What you need to do is store the ids and scores of all the students into arrays when you read the file.
Once you've computed the average grade, use the stored data to compute the letter grade for each student and put the 3 values (id, grade, letter grade) into the output file.
Ok I think I'm headed in the right direction. I just can't get the right data to save into the arrays for some reason. I was able to calculate the correct letter grade and output to new file, but I want to then read all 3 rows into arrays. When I try to run the menu options 2 and 3, the data it outputs is not right. :( why why why?
Sample "TestScores_Grades.txt" to read from:
10214 8 A-
19285 3 F
11753 5 B
11261 3 F
13893 -1 F
17611 7 A-
10587 3 F
15568 10 A
Line 26, incrementation of "s" should be outside the "if".
Line 61, grades should be a string array, not a char array.
Lines 100 and 119, it should be "==" and not "=".
Lines 98-99 and 117-118, this code should be outside the loop.