c - c 中的滚动总计循环

标签 c loops cumulative-sum

我试图让一个c程序询问我要购买多少件商品,而不是询问每件商品的价格,同时保持滚动总数,然后循环询问。我的循环工作正常,但在让它给我正确的输出时遇到问题。

这是我运行它时得到的输出。 http://i119.photobucket.com/albums/o134/halodude808/assignment2_zpsd46e84b8.jpg

#include <stdio.h>
#include <math.h>

int main(void)
{
    //variables used
    float penny = .01;
    float nickel = .05;
    float dime = .1;
    float quarter = .25;
    int items = 0;
    float paid= 0.0;
    float total = 0.0;
    float price =0.0;
    int counter =0.0;

    for (;;)
    {
        printf("Please enter the number of grocery items:");
        scanf("%d", &items);
        for (counter = 1; counter <= items; counter++)
        {
            printf("Please enter the price for item #%d:", counter);
            scanf("%f", &price);

            total += price;


        }
        printf("items = %d total = %f \n", &items, &total);
        getchar();
        getchar();

    }
}

最佳答案

改变

 printf("items = %f total = %f \n", &items, &total);

printf("items = %i total = %f \n", items, total);

此外,您可能需要考虑检查无效值(零、负数、字符等)。

关于c - c 中的滚动总计循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28637338/

相关文章:

c - 如何分配适量的内存(三)

c - 为什么这个程序总是从二叉搜索树中打印相同的节点

javascript - javascript foreach 循环、for..in 和 Angular forEach 循环之间的区别?

Java删除数组中的冗余项

python - 为什么实现随机词生成器需要累积频率和列表?

matlab - 写出一次取两个或三个乘积的总和

c - 奇怪的 'asm' 操作数有不可能的约束错误

c - C中链表的标准实现

algorithm - 如何为嵌套循环设置变量?

powerbi - PowerBI 日期累计发生次数?