C 如果语句不工作,程序立即结束
我正在制作气象员代码,但当我询问温度时,程序就结束了。这是密码C 如果语句不工作,程序立即结束,c,if-statement,C,If Statement,我正在制作气象员代码,但当我询问温度时,程序就结束了。这是密码 #include <stdio.h> int main(void) { int a; int b; int c; int d; printf("What Is Your Name?\n"); scanf("%s",&b); printf("Hi %s\n", &b); sleep(1); printf("Im Your Own Wea
#include <stdio.h>
int main(void)
{
int a;
int b;
int c;
int d;
printf("What Is Your Name?\n");
scanf("%s",&b);
printf("Hi %s\n", &b);
sleep(1);
printf("Im Your Own Weather Man\n");
sleep(1);
printf("Isn't That Great!\n");
sleep(1);
printf("Please Type A Number In\n");
sleep(1);
scanf("%d",&a);
//checking out the number you wrote
if(a<3)
{
printf("Its A Beatiful Day\n");
printf("%c\n",2);
}
if(a>3)
{
printf(":(\n");
}
if(a==3)
{
printf("Its Snowing!\n");
printf("%c\n",3);
sleep(5);
printf("Would you like to know the temprature?\n");
scanf("%s", &c);
}
if(c=='yes')
printf("20F\n");
return 0;
}
#包括
内部主(空)
{
INTA;
int b;
INTC;
int d;
printf(“您叫什么名字?\n”);
scanf(“%s”、&b);
printf(“高%s\n”和&b);
睡眠(1);
printf(“我是你自己的气象员”);
睡眠(1);
printf(“不是很好吗!\n”);
睡眠(1);
printf(“请在\n中键入一个数字”);
睡眠(1);
scanf(“%d”和“&a”);
//查看你写的号码
如果(a3)
{
printf(“:(\n”);
}
如果(a==3)
{
printf(“下雪了!\n”);
printf(“%c\n”,3);
睡眠(5);
printf(“您想知道温度吗?\n”);
scanf(“%s”、&c);
}
如果(c==‘是’)
printf(“20F\n”);
返回0;
}
我认为如果(c=='yes')我怎样才能打印出20F [编辑]感谢您帮助我,新代码就在下面。如果您需要,请随时使用,但请相信我
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main(void)
{
int a,b;
char c[5];
char d[30];
printf("What Is Your Name?\n");
scanf("%s",d);
printf("Hi %s\n", d);
sleep(1);
printf("Im Your Own Weather Man\n");
sleep(1);
printf("Isn't That Great!\n");
sleep(1);
printf("Please Type A Number In\n");
scanf("%d",&a);
//checking out the number you wrote
if(a<3)
{
printf("Its A Beatiful Day\n");
printf("%c\n",2);
}
if(a>3)
{
printf("Its A Cloudy Day");
printf(":(\n");
}
if(a==3)
{
printf("Its Snowing!\n");
printf("%c\n",3);
sleep(5);
printf("Would you like to know the temprature?\n");
scanf("%s",c);
}
if(strcmp("yes", c) == 0)
printf("20F\n");
return 0;
}
#包括
#包括
#包括
内部主(空)
{
INTA,b;
charc[5];
chard[30];
printf(“您叫什么名字?\n”);
scanf(“%s”,d);
printf(“高%s\n”,d);
睡眠(1);
printf(“我是你自己的气象员”);
睡眠(1);
printf(“不是很好吗!\n”);
睡眠(1);
printf(“请在\n中键入一个数字”);
scanf(“%d”和“&a”);
//查看你写的号码
如果(a3)
{
printf(“阴天”);
printf(“:(\n”);
}
如果(a==3)
{
printf(“下雪了!\n”);
printf(“%c\n”,3);
睡眠(5);
printf(“您想知道温度吗?\n”);
scanf(“%s”,c);
}
如果(strcmp(“是”,c)==0)
printf(“20F\n”);
返回0;
}
问题在于您正在使用以下行将字符串读入int:
scanf("%s", &c);
请尝试此操作,其中%d
显示输入类型为int
scanf("%d", &c);
问题在于您正在使用以下行将字符串读入int:
scanf("%s", &c);
请尝试此操作,其中%d
显示输入类型为int
scanf("%d", &c);
c
被定义为int
,但您将其视为char*
您还应该使用strcmp
而不是=
。您不能将字符串与=
运算符进行比较
将
c
的定义更改为:charc[128]
或其他任意长度。然后在执行scanf时删除&
操作符:scanf(“%s”,c”);
最后,将if(c='yes')
更改为if(strcmp(“yes”,c)==0)
c
被定义为int
,但您将其视为字符*
您还应该使用strcmp
而不是=
。您不能将字符串与=
运算符进行比较
将c
的定义更改为:charc[128]
或其他任意长度。然后在执行scanf:scanf(“%s”,c)”时删除&
操作符;
最后,将if(c='yes')
更改为if(strcmp(“yes”,c)==0)
您不能这样做,因为“yes”
是一个错误声明,即使它是一个类似“yes”
的字符串,您也不能在c中直接比较字符串。请使用内置库函数或编写自己的函数进行字符串比较/
您不能这样做,因为
'yes'
是一个错误声明,即使它是一个类似的“yes”
您无法在c中直接比较字符串。请使用内置库函数或编写自己的函数进行字符串比较相信我,在这段代码中,if
s是您最不担心的问题。我看到大量对scanf()的无效调用
感谢您提供的信息,该程序现在运行顺利,没有任何警告和错误显示我会循环所有这些吗相信我,if
s是您在这段代码中最不担心的问题。我看到大量对scanf()的无效调用
感谢您提供的信息,该程序现在运行顺利,没有任何警告和错误。我会循环所有这些吗?这不是唯一的一个。还有更多。这不是本例中具体问题的原因,但至少应该指出,c=='yes'
在c中不是有效的字符串比较。这不是唯一的一个re更多。这并不是本例中具体问题的原因,但至少应该指出,c=='yes'
在c中不是有效的字符串比较。