While Statement using a counter entering grades and output avg

The problem is is that when I compile the program it is allowing me to enter as many grades as I want and that is what I want but what I need to be able to do it have a session where I can enter grades for the section2. Also when the counter is calculating it is giving me 1 more than I should have counted.
I am a beginner so I am not perfect at this stuff. Thank you in advance.Ok, here is my code


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
#include <iostream>

using namespace std;

int main() 
{

	//declare variables

	double totalgrade1 = 0.0;
	double section1 = 0.0;
	double section2 = 0.0;
	double grade = 0.0;
	double avg_section1 = 0.0;
	double avg_section2 = 0.0;
	int count  = 0; //counter

		while  (count == 0)
		{
			cout << "Enter Grade for Section 1: ";
			cin >> grade;

			while (grade != -1)

			{
				totalgrade1 = totalgrade1 += grade; 
				count = count + 1;
				cout << "Enter the Next Grade for Section 1: ";
				cin >> grade;
			}

		}

		if (totalgrade1 > 0)
		{
			count = count + 1;
			avg_section1 = totalgrade1 / count;
			cout << "Average Grade for Section 1:  " << avg_section1 << endl;
			cout << "How many grades: " << count << endl;
		}
		else
		{
			cout << "Invalid Grade!" << endl;
		}
		// Counter
		count = count += 1;
		totalgrade1 = 0.0;
		count = 0;

		system("pause");
			return 0;
}


Thank you in advance.
Last edited on
line 18: should be while (count == 1) // Note ==
coder777, that is a good start. Thank you very much.

Now how do I incorporate section 2 into my equation? Plus I put a

cout << "How many grades: " << count << endl;

which is giving me one more than how many grades I tested it with.
I'd suggest not to place the green check mark if your issue isn't solved...

which is giving me one more than how many grades I tested it with.
This is due to line 36 where you increase the count without reason.

On line 46 you increase it again without any reason but that has no effect. It's just, writing it so count = count += 1; does not make too much sense.
You can write it so:

count = count + 1; count += 1; ++count; count++;
Topic archived. No new replies allowed.