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;
#include <iostream>
usingnamespace 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";
}
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.