Trying to get this program to take negative integers and display the average

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bclark29
    New Member
    • Sep 2013
    • 2

    Trying to get this program to take negative integers and display the average

    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;
        
    }
    Last edited by Rabbit; Sep 20 '13, 09:27 PM. Reason: Please use code tags when posting code or formatted data.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Works fine for me. What values were you inputting?

    Comment

    • bclark29
      New Member
      • Sep 2013
      • 2

      #3
      I've entered -4 multiple times. The code executes in such a way that I am only able to enter one other integer to be calculated for the average

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        Entered -4 multiple times for which input? How many numbers did you tell it you were going to input before you started entering -4? And now many -4s did you enter? I need specifics.

        Comment

        Working...