在 c 中滚动总计的循环

Loop with rolling total in c

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

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