I'm almost done with this assignment. All I need to do is to the calculate the average test score for each student given by a file. I also have to display their grade. The problem is that I keeping junk values for my averages. Then I set all four of my scores to 0 but that didn't work either. I can easily display the grades by myself but I want to calculate the averages first. This is where I'm stuck.
Note 1 : Four test scores equate to one student.
Note 2 : Here all the numbers that appear in the given text file.
Note 3: Details from my instructor: Each line represents the four tests taken by students in a class during the semester. You need to calculate the average for each student in the class by reading in the file. Display the average for each student along with their letter grade.
Edit: I have made some revisions on my code.
comment if you need me to include information that you feel is not present in this topic.
/*This program calculates a student's average test score, displays their
grade and then reads the file back to the user. */
#include <iostream> // Library with basic input and output functions.
#include <fstream> // Used to read/write files.
#include <iomanip> // Has the function setw.
usingnamespace std;
int main()
{
ifstream testscores; // Creating an object to output file.
testscores.open("grades.txt"); // Opening the file.
char grade[6] = {'A', 'B', 'C', 'D', 'F'}; // Student's grade.
int score1, score2, score3, score4; // Four test scores.
double average; // The average test score for each student.
cout << "These are the average test scores for each student.\n\n";
cout << "Averages Grade";
cout <<"\n-------- -----\n";
if (!testscores) // Check for errors
cout << "Error opening file.\n";
else
{
while(testscores >> score1 >> score2 >> score3 >> score4) /* A loop
that reads each number */
{
average = (score1 + score2 +score3 +score4) / 4.0;
cout << setw(2) << average << endl;
}
testscores.close(); // closing the file.
}
return 0;
}
1. then what I can I do? plus which line
2. will this work then while(testscores >> average)
{
// statements........
}
3. It was junk values before I decided to set each score variable to 0.
1) Use old-fashioned if statements to check if file is open.
2) Yes that is better, but that does not solves main problem: why do you read info from file in average? Last time I checked, your file did not contain averages.
1) alright gotcha.
2. here are the details from my instructor: Each line represents the four tests taken by students in a class during the semester. You need to calculate the average for each student in the class by reading in the file. Display the average for each student along with their letter grade.
I think I understand what you're saying. what I was thinking that the program was doing was reading all the numbers in the file and then taking four numbers in that file, placing those four values into
score1, score2, score3, and score4 and then performing an operation indicated by the formula.
If leaving each score variable by itself gives me junk values and making each score variable equals to 0 just me zero overall, then what can I do that will make the program extract integers from the file and place those numbers in the equation so that it displays the average for each student.