Noob here. I'm learning C. and I faced this problem I couldn't find the error/bug in my code, everytime it prints the average of the program the value of 2686776 whatever the input is. I'm using Dev-C++
#include <stdio.h>
int main () {
int loop, money, total, avg;
total = 0;
loop = 0;
while(loop < 4) {
printf("Money Spent");
scanf("%d", &money);
total = total + money;
loop = loop + 1;
}
avg = total / 4;
printf(" average %d", &avg);
getch();
}
You have to remove the & from the printf statement otherwise you print the address of this variable.
Also avg is int so it can't have decimal digits! (e.g. 4, 5 -> 9 -> avg = 4). So you have to change it to float or double
Remove & from the printf argument and better declare avg as float and change
avg = total / 4;
printf(" average %d", &avg);
to
avg = total / 4.0; // or (float)total / 4;
printf("Average: %f\n", avg);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With