Help writing a program?

I need help on how to determine the average of the test scores... if a -1 is entered for a test score grade it's an absence (so it should be ignored).. I know how to write this program if it's not in loop format, but because of the for loop, I don't know how to calculate the average and tell the program to ignore the -1 (I can't distinguish each test score, as var test1, test2, etc). For example, if the test scores are -1, 90, 80, 90. The calculation would be (90+80+90)/3 ... if there's two -1's then the grade is automatically an I.

What am I doing wrong?? It's not calculating correctly. It's the //Calculate the average part I need help with.

Also, why is not displaying the letter grade?


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
 #include <iostream>
using namespace std;
int main()
{
	int scores, stuID, testScore, exAbs = 0, total = 0;
	char choice, grade=0;

	//Ask user number of test scores
	cout << "Enter the number of scores for this semester: ";
	cin >> scores;
	
	//Begin OUTER loop
	do
	{
		cout << "=====================================================\n";
		cout << "Enter a student's ID: ";
		cin >> stuID;


               //Test scores
		for (int i=1; i<=scores; i++)
		{
			cout << "Enter a score: ";
			cin >> testScore;
		
			//Calculate average
			if (testScore == -1) exAbs++;
			else total = total + testScore;
			avg = (total) / (scores - exAbs);
		
		
		}
	
		//Determine letter grade
		{
			if (avg >= 90)
				grade == 'A';
			if (avg < 90 && avg >= 80)
				grade == 'B';
			if (avg < 80 && avg >= 70)
				grade == 'C';
			if (avg < 70 && avg >= 60)
				grade == 'D';
			if (avg <60 && avg >= 0)
				grade == 'F';
		}

		//Display ID, Avg, & Letter Grade
		cout << "ID=" << stuID << " ";
		cout << "Avg=" << avg << " ";
		cout << "Grade=" << grade << endl;
		cout << "=====================================================" << endl;

		//Ask user to continue loop
		cout << "Add a student (Y to continue, any other character to end): ";
		cin >> choice;
	} while (choice == 'Y' || choice == 'y');



	system("pause");
	return 0;
}
Last edited on
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
#include <iostream>
using namespace std;
int main()
{
	int scores, stuID, testScore, exAbs = 0, total = 0;
	char choice, grade=0;

	//Ask user number of test scores
	cout << "Enter the number of scores for this semester: ";
	cin >> scores;
	
	//Begin OUTER loop
	do
	{
		cout << "=====================================================\n";
		cout << "Enter a student's ID: ";
		cin >> stuID;


               //Test scores
		for (int i=1; i<=scores; i++)
		{
			cout << "Enter a score: ";
			cin >> testScore;
		
			//Calculate average
			if (testScore == -1) exAbs++;
			else total = total + testScore;
                        // never declared avg
			avg = (total) / (scores - exAbs); 
                       // missing curly bracket
		
		
		}
	
		//Determine letter grade
		{
                       // Should set equal, not check if equivalent
			if (avg >= 90)
				grade == 'A';
			if (avg < 90 && avg >= 80)
				grade == 'B';
			if (avg < 80 && avg >= 70)
				grade == 'C';
			if (avg < 70 && avg >= 60)
				grade == 'D';
			if (avg <60 && avg >= 0)
				grade == 'F';
		}

		//Display ID, Avg, & Letter Grade
		cout << "ID=" << stuID << " ";
		cout << "Avg=" << avg << " ";
		cout << "Grade=" << grade << endl;
		cout << "=====================================================" << endl;

		//Ask user to continue loop
		cout << "Add a student (Y to continue, any other character to end): ";
		cin >> choice;
	} while (choice == 'Y' || choice == 'y');



	system("pause");
	return 0;
}
Topic archived. No new replies allowed.