0

我对 Stack Overflow 和 C 编程相当陌生,希望我不会惹恼任何缺乏知识的人。

我正在为 Kickstarter 项目创建一个平均计算器,我想知道为什么下面的方法不起作用。不是平均水平,但是如果您要输入 1 个支持者并每天承诺 10 美元,为什么支持者和资金会翻倍,

    #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();

}

但以下工作正常

#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();

}
4

2 回答 2

7

在第一种情况下,每次计算后,当您获得新的用户输入时,您会覆盖该值:

scanf("%d\n%d", &backers, &money);

例如:

line                  | backers    | money
----------------------+------------+------
user input 5 3        | 5          | 3
backers += backers    | 10         | 3
money += money        | 10         | 6
user input 8 6        | 8          | 6
backers += backers    | 16         | 6
money += money        | 16         | 12

现在,在第二个示例中,您不覆盖这些值,而是将它们添加到总和中:

line                  | backers    | money    | tbackers   | tmoney    
----------------------+------------+----------+------------+---------
user input 5 3        | 5          | 3        | 0          | 0
tbackers += backers   | 5          | 3        | 5          | 0
tmoney += money       | 5          | 3        | 5          | 3
user input 8 6        | 8          | 6        | 5          | 3
tbackers += backers   | 8          | 6        | 13         | 3
tmoney += money       | 8          | 6        | 13         | 9
于 2012-05-18T07:18:30.083 回答
1

“不起作用”到底是什么意思?我无法想象它不起作用。

money += money将 的值添加money到 variable 的内容中money,有效地使其值加倍。

如果您想实现其他目标,则必须表达其他内容,就像您在第二个示例中所做的那样:为总和设置一个变量,为用户输入设置一个变量。如果你把它们混合起来,你最终会得到破坏(覆盖)的数据。

如果您使用的是 C99 或者您的编译器允许,您可以将变量声明在靠近您需要它们的位置:

[...]

while(loopcount<5){
   int money = 0;
   int average = 0;


   //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++;
}
[...]

这将使用backers和限制在循环money的范围内while,因此它们不能泄漏。

于 2012-05-18T07:18:57.330 回答