Everything seems perfectly normal to me, but it doesn't work...
I'm making a prog that stores the numbers you enter and then - when you enter a negative value - calculates the average.
but when I let it play, it gives some totally wrong output
#include <iostream>
usingnamespace std;
int getal[128];
int i;
int a = 0;
int b;
double c;
int main ()
{
cout<<"Dit programma telt alle getallen op en berekent dan het gemiddelde.\nOm het te stoppen, geef een negatief getal in.\n";
while (true) {
cin >> i;
if (i>0)
{
getal[a] = i;
b += getal[a];
a++;
}
else
{
c = (b/a);
cout<<"Average is "<<c<<".";
}
}
return 0;
}
Why don't you just replay the program in your head step by step? Then you'll quickly see why it doesn't work.
Instead of accumulating a sum in b, you're always overwriting it with the last entered value.