trouble with functions

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;
}
 
Last edited on
Your issue is here, on lines 32 and 33:
1
2
		ofstream outputFile;
		outputFile.open("avegrade.txt");


You are re-opening the file every time you execute that loop. Every time you open it, the file gets cleared. You should move that outside the loop so that it only gets opened once.
After you are finished with the file, don't forget to close to stream.
Thank you
It works now.
Thanks again
line 38 getline (cin, studentName) will fix your student name problem.

Initialization for a function should be done like this;
1
2
3
4
5
6
7
8
9
10
11
void grade2 (int numStudents, int numTests) {
         ofstream outputFile;
	 outputFile.open("avegrade.txt");

	// This is so only one decimal will be displayed as per specification	
	outputFile << fixed << showpoint << setprecision(1);

	for (int student = 1; student <= numStudents; student++)	{
        // Existing code here
       }	
}


Before you come around and ask for another students name, input stream should be flushed with cin.ignore (48,'\n') otherwise when your asked for the next students name, it already reads EOF.
Last edited on
Topic archived. No new replies allowed.