gpt4 book ai didi

C语言编程: the app wont stop after typing x

转载 作者:行者123 更新时间:2023-11-30 15:23:01 26 4
gpt4 key购买 nike

我制作了这个程序,它按照我想要的方式工作,但是当我输入 x 时它应该停止,但它没有谁能告诉我为什么吗?是否有任何快捷方式或更小的方法来输入此代码?提前致谢。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int meat[6];
int i;
int total =0;
int avg;
char what[20];
char end;
int days = 0;
char food[20];
char drink[20];
printf("what thing do u want to calculate the average of?(food/drink)\n");
scanf(" %s", &what);
if(strcmp(what, "food")==0)
{
printf("what sort of food did u ate ?\n");
scanf(" %s", food);
}
if(strcmp(what, "drink")==0)
{
printf("what sort of drink did u drank ?\n");
scanf(" %s", drink);
}
for(i=0; i<6; i++)
{
days++;
if(strcmp(what, "food")==0)
{
printf("how many %s did u ate in day %d\n", food, i+1);
}
else if(strcmp(what, "drink")==0)
{
printf("how many liters of %s did u drank in day %d\n", drink, i+1);
}
scanf(" %d", &meat[i]);
total += meat[i];
printf("if u want to continue type y\n");

printf("type x if u want to finish\n");
scanf(" %c", &end);
if((end = 'y')&&(i<5))
{
continue;
}
else if(end = 'x' && strcmp(what, "drink")==0)
{
avg = total / days;
printf("\nyour average amount of liters of %s you had %d\t the total is %d\n", drink, avg, total);
}
else if(end = 'x' && strcmp(what, "food")==0)
{
avg = total / days;
printf("\nyour average amount of %s you had %d\t the total is %d\n", food, avg, total);
}
break;
}
if(strcmp(what, food)==0)
{
printf("\nyour average amount of %s you had is %d\t the total is %d\n", food, avg, total);
}
else if(strcmp(what, drink)==0)
{
printf("\nyour average amount of liters of %s you had is %d\t the total is %d\n", drink, avg, total);
}

return 0;
}

最佳答案

else if(end = 'x' ...

应该是:

else if(end == 'x' ...

您可以使用 == 来测试 if 语句中的相等性。您在代码中的几个位置出现了这种情况,这是无意中执行的分配,而不是您想要通过比较用户输入是否等于特定字符来实现的目标。

将此处的 = 替换为 ==:

else if(end = 'x' && strcmp(what, "drink")==0)

这里:

else if(end = 'x' && strcmp(what, "food")==0)

这里:

if((end = 'y')&&(i<5))

关于C语言编程: the app wont stop after typing x,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28930954/

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