循环滚动总数为c

时间:2015-02-20 20:07:51

标签: c loops running-total

我正在尝试获得一个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();

    }
}

1 个答案:

答案 0 :(得分:2)

更改

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

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

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