【问题标题】:The last "if" statement is not working when I run the program当我运行程序时,最后一个“if”语句不起作用
【发布时间】:2020-08-07 21:24:17
【问题描述】:

每次我运行程序时,最后一个“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;
}

【问题讨论】:

  • %c 只扫描一个字符,使用if (ans == 'n') { ...char ans[4]; scanf(" %s", ans); if (strcmp(ans, "no") == 0){ ...
  • 激活警告,你会得到指出的问题。
  • ans == 'no' 不起作用;正如大卫所说,使用 strcmp。
  • 已经提到字符串比较不能这样工作。除此之外,'no' 不是字符串。它是一个多字符文字,它是一个具有实现定义值的整数。请改用"no"

标签: c loops for-loop break


【解决方案1】:

使用if (ans == 'n')。如果要使用"no"这个词,则必须将变量ans的类型改为char数组,并使用strcmp()方法比较字符串。

【讨论】:

    【解决方案2】:

    您使用了%c,它用于字符。 请改用%s

    【讨论】:

      【解决方案3】:

      在 c 编程中,单引号(即“c”)用于字符,双引号(即“c”)用于字符串。在双引号中最后一个字符为 NULL。

      注意:我们不能在单引号中保留两个字符,例如 'no'。

      在您的情况下,首先将 ans 声明为字符数组(即字符串)。

      char ans[SIZE_AS_PER_REQUIREMENT];
      

      To take input在此,

      scanf("%s",ans);
      

      为了更好的用户体验,在输入之前给用户一个适当的信息。

      printf("\n Do you want to continue(yes/no)?");
      

      现在将用户的答案与程序的条件进行比较,我们有 C 语言字符串库(即string.h),在使用任何 C 语言内置的字符串函数之前包含它。

      #include <string.h>
      

      并根据要求使用任何字符串函数strcmpstricmp。这里我将使用stricmp,因为用户可能会输入“no”/“No”/“NO”。 stricmp 忽略大小写。

      stricmp(string1,string2)
      

      返回

      • 如果 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;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-06-19
        • 1970-01-01
        • 2022-06-14
        • 2022-07-01
        • 2016-02-17
        • 1970-01-01
        • 1970-01-01
        • 2014-03-21
        相关资源
        最近更新 更多