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.