gpt4 book ai didi

C编程错误if语句

转载 作者:太空宇宙 更新时间:2023-11-04 02:00:16 24 4
gpt4 key购买 nike

我有这个程序,直到最后它都运行良好。当我询问用户他/她是否想要打折并且用户输入 y/n 时,程序只是停止并且不会转到 if 语句。有什么帮助吗?谢谢:)

int calculate(){

printf("Do you want to make a discount? y/n \n");
scanf("%c",&ask);

if (ask == 'y')
{
printf("Enter the actual money that you want to make a discount\n");
scanf("%f",&disc);
discount= (total * (disc/100));
net= (total - discount);
printf("The total bill is: %.2f\n", net);


}
else
if(ask == 'n')
{
printf("The total bill is: %.2f\n", total);


}
//menu();
return 0;

}

最佳答案

改变这个

scanf("%c",&ask);

scanf(" %c",&ask);

明确忽略任何剩余的空白。

关于C编程错误if语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28119589/

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