I’m fairly new to Stack Overflow as well as C programming, hope I don’t annoy anyone with lack of knowledge.
I’m creating an average calculator for Kickstarter projects, what I was wondering is why the method below doesn’t work. Not the average but why backers and money come out doubled if you were to enter 1 backer and $10 pledged each day,
#include <stdio.h>
#include <conio.h>
int main(void){
int loopcount = 0;
int backers = 0;
int money = 0;
int average = 0;
int tbackers = 0;
int tmoney = 0;
while(loopcount<5){
//Ask for # of backers and total money pledged.
printf("Please Enter the number of backers today, then the total money pledged today:\n");
scanf("%d\n%d", &backers, &money);
//
backers += backers;
money += money;
loopcount++;
}
//average = tmoney / tbackers;
printf("There were %d backers and the total collected was $%d.\nSo the average amount pledged was $%d", backers, money, average);
getch();
}
but the following works fine
#include <stdio.h>
#include <conio.h>
int main(void){
int loopcount = 0;
int backers = 0;
int money = 0;
int average = 0;
int tbackers = 0;
int tmoney = 0;
while(loopcount<5){
//Ask for # of backers and total money pledged.
printf("Please Enter the number of backers today, then the total money pledged today:\n");
scanf("%d\n%d", &backers, &money);
//
tbackers += backers;
tmoney += money;
loopcount++;
}
//average = tmoney / tbackers;
printf("There were %d backers and the total collected was $%d.\nSo the average amount pledged was $%d", tbackers, tmoney, average);
getch();
}
In the first case, after each calculation, you override the value when you get a new user input:
For example:
Now, in the second example, you don’t override the values, but add them to the sum: