Nested loops

Can someone tell me how I can get my loop to cout the grades that the user inputs. I tried putting a cout statement outside the while loop but it only shows the last grade that the user input.

<code>

int choice,grad,count,sum;
cin >> choice;

if (choice == 1)
{
while (grad >= 0)
{
cout <<"Enter the necessary grades.\n";
cin >>grad;
sum = sum + grad;
count ++;
}
cin >> choice;
}

</code>

Also in order for the user to stop inputting grades he/she must input a negative number how do I prevent that from been added in the sum and count?? I tried doing grad !='-' but it didn't work out. Thanks all help is appreciate it.
I do not see any sense in the statement before the loop

cin >> choice;

I do not even see a sense to use variable choice because there is no any choice.:)

So I would write

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cout <<"Enter the necessary grades.\n";

while ( true )
{
   int grad = -1;

   cin >>grad;

   if ( grad < 0 ) break;

   count ++;
   sum += grad;
}

cout << "sum = " << sum << endl;




Sorry I just realized half of the code didn't show up this is my actual program.

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
#include <iostream>
using namespace std;

void Display_Menu();

int main ()
{
    Display_Menu();

	int choice,grad,count;
	int sum = 0;
	cin >> choice;
 
    if (choice == 1)
		{
			while (grad >= 0)
			{
		     cout <<"Enter the necessary grades.\n";
			 cin >>grad;
			 sum = sum + grad;
			}
		  cout << sum;
		  cout <<"Enter you're next choice\n";
		  cin >> choice;
	    }
	if (choice == 2)
	{
	  int average;
	  average = sum/count;
	  cout << average;
	}
	system ("pause");
}

	
void Display_Menu()
{
	cout << " Option 1 : Enter grades ( Quit with a negative value)\n";
	cout << " Option 2 : Display the Mean(average) for the grades\n";
	cout << " Option 3 : Display the standard deviation for the grades\n";
	cout << " Option 4 : Display the high and low grades\n";
	cout << " Option 5 : Enter 'q' to quit\n";
	cout << "Enter you're choice (1-5)\n";
}






Sorry, I wasn't trying to be confusing.
I think I figured it out

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void Display_Menu();

int main ()
{
    Display_Menu();

	int choice,grad,count,sum;
	cin >> choice;
 
    if (choice == 1)
		{
			while (grad >= 0)
			{
		     cout <<"Enter the necessary grades.\n";
			 cin >>grad;
			 if (grad >=0)
			 {
				 sum = sum + grad;
				 count++;
			 }
			}
		  cout <<"Enter you're next choice\n";
		  cin >> choice;
	    }


That stop the while loop when the user enters any negative value and the if (grad >=0) only counts/sum the actual grades the user entered not the negative value.
This loop

while (grad >= 0)


is invalid because grad was not initially initialized. So the behavior is undefined.

And I do not see where there is "cout the grades that the user inputs" in your menu.

If you need to add such choice in your menu you should keep somewhere entered grads. I would suggest to use std::forward_list
Topic archived. No new replies allowed.