Hello
This is my assignment. Write a program that asks for a name and 3 grades from 4 students. It should then store in a text file the students name, the averages of the grades (to 1 decimal point), and the appropriate letter grade.
There should be one function that uses a loop to get three grades, with prompts and validation, and returns the average as a float return type.
There should be a second function that is passed the average as a float argument. The grade should be determined as follows.
>=90 A, >=80 B, >=70 C, everything else F.
The grade should be returned as a char return type.
the part I am having issues with is saving to the file.
It will only save the last name and grade to the file.
Any help would be appreciated.
Thanks
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
char grade1(float);
void grade2(int, int);
int main ()
{
int numStudents = 4;
int numTests = 3;
grade2 (numStudents, numTests);
return 0;
}
void grade2 (int numStudents, int numTests)
{
for (int student = 1; student <= numStudents; student++)
{
float total = 0;
float average;
string studentName;
ofstream outputFile;
outputFile.open("avegrade.txt");
cout << fixed << showpoint << setprecision(1);
cout << "What is the students name? " << endl;
cin >> studentName;
outputFile << studentName;
for (int test = 1; test <= numTests; test++)
{
float score;
cout << "Enter score: " << endl;
cin >> score;
while (score < 1 || score > 100)
{
cout << "Invalid Entry, Score must be between 0 and 100. " << endl;
cin >> score;
}
total += score;
}
average = total / numTests;
cout << "The average score for " << studentName << " is " << average << " " << grade1(average) << endl;
outputFile << " " << average << grade1(average) << " " << endl;
}
}
char grade1 (float average)
{
char letter;
if (average >= 90)
letter = 'A';
else if (average >= 80)
letter = 'B';
else if (average >= 70)
letter = 'C';
else
letter = 'F';
return letter;
}
|