User input how many times to run average

This is a discussion on User input how many times to run average within the C++ Programming forums, part of the General Programming Boards category; Hi, This is first class in C++ and I'm having logical calculation problem as below. User needs to input how ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    2

    Thumbs down User input how many times to run average

    Hi,

    This is first class in C++ and I'm having logical calculation problem as below.

    User needs to input how many times to run average, for example 6. User is prompted to enter six inputs, and he/she enters "0" for average to be computed. This does not give correct result as you can see below. I also would NOT like to limit the "while (count <= 10) " but it does not work if I don't input anything. Also if I use "MAXNUMS = 10" it also limits user to ten.

    Anyway - regardless of while loop I'm not getting a correct answer.

    I would appreaciate if someone can be able to point where Im going wrong. Im suspecting my while loop is not correct. Please help.

    =====
    Code:
    ,
    #include <iostream.h>
    #include <iomanip.h>
    
    
    int main()
    {
    
    
    //const int MAXNUMS = 0;
    int count;
    float num, nu_avg, average, total;
    
    total = 0.0;
    count = 0;
    
    cout << endl << endl // Make some space
    
       << endl << "\nPlease type in the total number of data values to be averaged"
       << endl << " Enter a zero (0) when you have finished entering numbers and want to compute the"
       << endl << " average of the inputted numbers: " ;
    
       cin >> nu_avg;
    
    //cout << "\nPlease type in the total number of data values to be averaged:  "
          // << " numbers.\n";
       //<< numbers << " numbers.\n";
      count ++;
    
    count = 0;
    //total = 0;
    
    while (count <= 10)
    {
         
        count ++;
    
         cout << "\nEnter a number: ";
      
      cin  >> num;
      total = total + num;
      average = total / count;
      cout << "The average is now "<< average;
      //count++;
    } 
    
         cout << "\nThe final average is " << average << endl;
    
    return 0;
    }
    ------
    output
    -------
    Please type in the total number of data values to be averaged
    Enter a zero (0) when you have finished entering numbers and want to compute th
    e
    average of the inputted numbers: 4

    Enter a number: 5
    The average is now 5
    Enter a number: 6
    The average is now 5.5
    Enter a number: 6
    The average is now 5.66667
    Enter a number: 5
    The average is now 5.5
    Enter a number: 4
    The average is now 5.2
    Enter a number: 0

    The average is now 4.33333 // Wrong answer !!!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Why does your while loop go to 10 when they told you what number to stop at the first time around? For that matter, what's the point of the 0 when they told you what number to stop at?

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    2
    Number "10" is just something I was trying to experiment. Even count has no given number, just experimenting. Keyboards reads the count once user types in.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to limit user input to a certain number of digits?
    By NewbGuy in forum C Programming
    Replies: 7
    Last Post: 05-08-2009, 09:57 PM
  2. timed user input
    By sainiabhishek in forum C Programming
    Replies: 4
    Last Post: 04-01-2009, 11:59 AM
  3. newbie question regarding user input
    By cantore in forum C Programming
    Replies: 4
    Last Post: 03-05-2006, 07:57 PM
  4. Ending user input with # character
    By jowatkins in forum C++ Programming
    Replies: 2
    Last Post: 04-27-2004, 10:41 AM
  5. Replies: 4
    Last Post: 04-21-2004, 04:18 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21