gpt4 book ai didi

c - c 中的滚动总计循环

转载 作者:行者123 更新时间:2023-11-30 18:55:08 25 4
gpt4 key购买 nike

我试图让一个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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com