Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 最后一句话;如果;运行程序时语句不起作用_C_Loops_For Loop_Break - Fatal编程技术网

C 最后一句话;如果;运行程序时语句不起作用

C 最后一句话;如果;运行程序时语句不起作用,c,loops,for-loop,break,C,Loops,For Loop,Break,每次我运行程序时,最后一个“if”语句都不起作用,这意味着如果我键入“no”,循环就不会中断。有人能帮我吗 #include <stdio.h> int main() { int age, i; char ans; for (i = 0; i < 3; i++) { printf("\n enter your age:"); scanf("%d", &age); if (age > 18) { printf("

每次我运行程序时,最后一个“if”语句都不起作用,这意味着如果我键入“no”,循环就不会中断。有人能帮我吗

#include <stdio.h>

int main() {
  int age, i;
  char ans;

  for (i = 0; i < 3; i++) {
    printf("\n enter your age:");
    scanf("%d", &age);

    if (age > 18) {
      printf("your age is %d, you are allowed to enter", age);
    } else if (age == 18) {
      printf("I don't know what to do with you");
    } else {
      printf("your age is %d, you are not allowed to go in", age);
    }

    printf("\n continue?");
    scanf(" %c", &ans);

    if (ans == 'no') {  // <-- here
      break;
    } else {
      continue;
    }
  }

  return 0;
}
#包括
int main(){
国际年龄,i;
查尔安斯;
对于(i=0;i<3;i++){
printf(“\n输入您的年龄:”);
scanf(“%d”和年龄);
如果(年龄>18岁){
printf(“您的年龄是%d,您可以输入”,年龄);
}否则,如果(年龄==18岁){
printf(“我不知道该拿你怎么办”);
}否则{
printf(“您的年龄是%d,您不允许进入”,年龄);
}
printf(“\n是否继续?”);
scanf(“%c”和“&ans”);

如果(ans=='no'){/使用
if(ans=='n')
。如果要使用单词
“no”
,必须将变量
ans
的类型更改为char数组,并使用
strcmp()
方法来比较字符串。

您使用了
%c
,它用于字符。
相反,在c编程中使用
%s
单引号(即“c”)用于字符,双引号(即“c”)用于字符串。在双引号中,最后一个字符为空

注意:我们不能在单引号中保留两个字符,如“否”

在本例中,首先将ans声明为字符数组(即字符串)

在这方面,

scanf("%s",ans);
为了在输入之前获得更好的用户体验,请向用户发送适当的消息

printf("\n Do you want to continue(yes/no)?");
现在,为了比较用户的答案和程序的条件,我们有了C语言字符串库(即),在使用任何C语言内置的字符串函数之前包含它

#include <string.h>
它回来了

  • 如果string1小于string2,则为负数
  • 如果string1等效于string2,则为零
  • 如果string1大于string2,则为正数
因此,对于我们的情况,我们检查零

请看下面的程序,我刚刚在您的代码中添加了这些

#include <stdio.h>
#include <string.h>
int main() {
  int age, i;
  char ans[5];//declare ans as character array

  for (i = 0; i < 3; i++) {
    printf("\n enter your age:");
    scanf("%d", &age);

    if (age > 18) {
      printf("your age is %d, you are allowed to enter", age);
    } else if (age == 18) {
      printf("I don't know what to do with you");
    } else {
      printf("your age is %d, you are not allowed to go in", age);
    }

    printf("\n Do you want to continue(yes/no)?");
    scanf("%s",ans);//take input as string in ans, its character array

    if (stricmp(ans,"no") == 0) {  // 0 means both are equal
      break;
    } else {
      continue;
    }
  }

  return 0;
}
#包括
#包括
int main(){
国际年龄,i;
char ans[5];//将ans声明为字符数组
对于(i=0;i<3;i++){
printf(“\n输入您的年龄:”);
scanf(“%d”和年龄);
如果(年龄>18岁){
printf(“您的年龄是%d,您可以输入”,年龄);
}否则,如果(年龄==18岁){
printf(“我不知道该拿你怎么办”);
}否则{
printf(“您的年龄是%d,您不允许进入”,年龄);
}
printf(“\n是否继续(是/否)?”;
scanf(“%s”,ans);//将输入作为ans中的字符串,其字符数组
如果(stricmp(ans,“no”)==0{//0表示两者相等
打破
}否则{
继续;
}
}
返回0;
}

%c
只扫描一个字符,使用
if(ans='n'){…
char-ans[4];scanf(“%s”,ans);if(strcmp(ans,“no”)==0){…
激活警告,您会发现问题。ans=='no'不起作用;正如David所说,使用strcmp。前面已经提到字符串比较不起作用。除此之外,
'no'
不是字符串。它是一个多字符的文本,是一个具有实现定义值的整数。请改用
“no”
stricmp(string1,string2)
#include <stdio.h>
#include <string.h>
int main() {
  int age, i;
  char ans[5];//declare ans as character array

  for (i = 0; i < 3; i++) {
    printf("\n enter your age:");
    scanf("%d", &age);

    if (age > 18) {
      printf("your age is %d, you are allowed to enter", age);
    } else if (age == 18) {
      printf("I don't know what to do with you");
    } else {
      printf("your age is %d, you are not allowed to go in", age);
    }

    printf("\n Do you want to continue(yes/no)?");
    scanf("%s",ans);//take input as string in ans, its character array

    if (stricmp(ans,"no") == 0) {  // 0 means both are equal
      break;
    } else {
      continue;
    }
  }

  return 0;
}