【问题标题】:error in if condition even i enter code correctly in c programming即使我在 c 编程中正确输入代码,if 条件也会出错
【发布时间】:2013-02-26 15:31:34
【问题描述】:
#include <stdio.h>
#include <process.h>

int main()
{
     int check;
     int enter[7];
     int i,j;

     printf("enter any 7 number to be stored");
     for(i = 0; i < 7; i++)
           scanf("%d" ,&enter[i]);
     printf("\nenter any number to check:");
     scanf("%d" ,&check);
     for (i = 0; i < 7; i++)
     {
           if (enter[i]=check)
           {
                printf("your entry is valid");
                exit(0);
           }
           else if(enter[6]!=check)
           {
                 printf("your entry is not valid");
                 exit(0);
           }
           else
                 continue;
     }
     return 0;
}     

这执行没有错误但不能正常工作..总是打印出输入是有效的..即使我输入了不在数组中的数字:(

【问题讨论】:

    标签: c arrays if-statement for-loop


    【解决方案1】:

    这是赋值,而不是相等:

    if (enter[i]=check)
    

    改为:

    if (enter[i] == check)
    

    另外,经常检查输入操作的结果:

    if (1 != scanf("%d" ,&enter[i]))
    {
        /* Handle invalid value. */
    }
    

    确保后续代码对已赋值的变量进行操作。

    【讨论】:

    • 我把它设为: if (enter[i]== check) 。但它开始打印“您的输入无效”。那我该怎么办
    • @RahulSubedi,检查scanf() 的返回值以确保已读取某些内容并打印出== 之前的值以查看实际值。
    【解决方案2】:

    这一行

    if (enter[i]=check)
    

    不符合您的预期。你可能是说

    if (enter[i]==check)
    

    赋值是有效的 C,但不是检查相等性,而是设置 enter[i] 等于 check,然后 然后 检查 check 的值是否为零。如果它不为零,则条件成功,而不管enter[i] 的初始值如何。如果check 为零,则条件失败, - 同样,不管enter[i] 的初始值如何。这是一个非常常见的错误;许多编译器会发出警告以提醒您注意这种情况。

    【讨论】:

      【解决方案3】:

      = 是赋值运算符,根本不是相等。正在做:

      if (enter[i]=check)
      

      enter[i] 将取值check,然后检查enter[i] 是否为非零值。

      if (enter[i] == check)
      

      【讨论】:

        【解决方案4】:
        enter[i]==check)// 2 for compare
        

        【讨论】:

          【解决方案5】:

          正确使用 = 是赋值运算符,而 == 是测试相等性

          【讨论】:

            【解决方案6】:
            #include<stdio.h>
            #include<process.h>
            int main()
            
            {
            int check;
            int enter[7];
            int i,j;
            printf("enter any 7 number to be stored");
            for(i=0;i<7;i++)
            {
                scanf("%d" ,&enter[i]);
            
            }
            printf("\nenter any number to check:");
            scanf("%d" ,&check);
            for (i=0;i<7;i++)
            {
               // printf("\nvalue of i is %d\n" ,i);
                if (check==enter[i])
                {
                    printf("your entry is valid");
                    exit(0);
                }
                else if(enter[i]!=check && i==6)
                {
            
                    printf("your entry is not valid");
                    exit(0);
                }
                else
                continue;
            }
            return 0;
            }
            
             now i got it all right . thanks :)
            

            【讨论】:

              猜你喜欢
              • 2021-11-28
              • 2013-11-09
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2020-09-01
              • 1970-01-01
              • 1970-01-01
              • 2015-07-12
              相关资源
              最近更新 更多