gpt4 book ai didi

c - 为什么我的 float 打印为 0,即使我使用 scanf 为它输入 13.5?

转载 作者:太空狗 更新时间:2023-10-29 17:19:38 24 4
gpt4 key购买 nike

我无法弄清楚为什么当我将浮点变量作为数字输入时,它一直打印出 0。

代码:

int num, month, day, year;
float price[10000];


printf("Enter item number: \n");
scanf("%d", &num);
printf("Enter unit price: \n");
scanf("%f", &price);
printf("Enter purchase date (mm/dd/yyyy): \n");
scanf("%d/%d/%d", &month, &day, &year);

printf("Item\t\tUnit\t\tPurchase\n");
printf(" \t\tPrice\t\tDate\n");
printf("%d ", num);
printf("$%.2f ", price);
printf(" %d/%d/%d\n", month, day, year);
return 0;

我输入 555 作为商品编号,13.5 作为价格,10/24/2010 作为日期。当我这样做时,它打印出我的价格是 0.00 美元。它对我输入的任何数字都这样做。为什么?

最佳答案

你不能像这样插入数组值 -

scanf("%f", &price);

使用 for 循环将值插入数组价格 -

for(i=0; i<sizeWhatYouWant; i++){
scanf("%f", &price[i]);
}

或者只是将声明 float price[10000] 更改为 -

float price;

关于c - 为什么我的 float 打印为 0,即使我使用 scanf 为它输入 13.5?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28201482/

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