gpt4 book ai didi

c - 如何在char中使用For循环?

转载 作者:太空宇宙 更新时间:2023-11-03 23:47:16 25 4
gpt4 key购买 nike

  • 通过使用 for 循环。我想让用户输入他们想要订购的柠檬水的数量。然后我的程序将计算税金、小计和总计。之后应该让他们通过输入“y”或“n”继续。

  • 当我编译这些代码时。我得到的只是空白。我的代码有什么问题?

所以我做了一些改变。但是我不知道如果他们要求继续我输入'n'如何让它停止程序。

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>

void main()
{
int NumCups;
float SubTot, Tot = 0, Tax;
char more;

printf("Enter the number of cups:");
scanf("%d", &NumCups);
SubTot = NumCups*1.29;
Tax = SubTot*0.0825;
Tot = SubTot + Tax;

printf("Subtotal:%0.2f\nTax:%0.2f\nTotal:%0.2f\n", SubTot, Tax, Tot);
fflush(stdin);
printf("Thank you.\nWould you like to order more lemonade?\n");

for (; scanf("%c", &more);)
{
printf("Enter the number of cups:");
scanf("%d", &NumCups);

SubTot = NumCups*1.29;
Tax = SubTot*0.0825;
Tot += SubTot + Tax;
fflush(stdin);
printf("Subtotal:%0.2f\nTax:%0.2f\nTotal:%0.2f\n", SubTot, Tax, Tot);
printf("Thank you.\nWould you like to order more lemonade?\n");
}
system("pause");
}

最佳答案

试试这个:

 for (; scanf("%c", &more)&&more=='y';)

只有在使用 y 字符时,此更改才会让您继续。

关于c - 如何在char中使用For循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29859448/

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