我正试图让一个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);
此外,您可能需要考虑检查无效值(零、负数、字符等)。