Simple average calculation

Posted by sasquatch90 on Stack Overflow See other posts from Stack Overflow or by sasquatch90
Published on 2010-05-19T21:28:02Z Indexed on 2010/05/19 21:30 UTC
Read the original article Hit count: 227

Filed under:
|

I'm trying to write program calculating average of given numbers stored in an array. Amount of numbers should be not more than 100, and user should input them until a !int variable is given :

#include <iostream>
#include <conio.h>
using namespace std;

double average(int tab[], int i){

    int sum=0;

    for(int j=0; j<i; ++j){
            sum+=tab[j];
    }
    return (double)sum/i;

}

int main()
{
    int tab[100];
    int n=0;   
    int number=0;


    do {
       if(n < 100){
           cout << "Give " << n+1 << " number : ";
           cin >> number;
           tab[n]=number;
           number=0;
           ++n;       
       }
       else{
            break;
       }
    } while( !isdigit(number) );      

    cout << average(tab, n) << endl;

    getch();
    return 0;
}

Why after giving char, it prints me 'Give n number:' for all empty cells of my array ? It should end and use only given numbers.

© Stack Overflow or respective owner

Related posts about c++

Related posts about average