The loop will take positive integers just fine but when entering a negative integer the loop only runs once then computes the average of the only two inputs I am allowed to enter. It has to be my boolean expression for the while, or maybe I just need to add a nested if statement...
Code:
#include<iostream>
using namespace std;
int main(void)
{
int x;
int count = 0;
int number_of_values;
double sum = 0, average = 0;
char answer;
cout<<" Enter number of values to be read <Enter>:"<<endl;
cin>> number_of_values;
do //Adding the do while loop here changed the syntax.
{
cout<<"\n Enter a grade <Enter>:";
cin>> x;
sum = sum + x;
count++;
}
while(count < number_of_values); //Couldn't get the program to spit out negative numbers. Came pretty close.
if(number_of_values == 0)
cout<<" You have entered 0 numbers. No average will be computed. Bye! \n";
else
{
average = sum/number_of_values;
cout<<" The average of these "<<number_of_values<<" grade(s) is "<<average<<endl;
}
cout<<" Have a nice day!\n";
system("PAUSE");
return 0;
}
Comment