我的代码无法正常工作,我认为它的逻辑错误,但我没有得到它

My code is not working properly,its logical error i think but i'm not getting it

本文关键字:错误 代码 常工作 工作 我的      更新时间:2023-10-16

它无法正常工作,因为我希望它工作..根据我的想法,下面给出的代码总是正确的,我不知道为什么,但如果条件总是为真并且数字匹配..名为CNIC的变量是这样全局声明的(char CNIC[15])。

  private:
     char ch;
     char str[256];
     std::fstream file1;
  public:
    void verify()
    {
          cout<<"Enter your CNIC number for verifivation : ";
          for(int i=0;i<15;i++)                    
          {
                   CNIC[i] = getche();
          }
          file1.open("D:\UOL\OoP\Nadra database.txt",ios::in);
          check = false;
          while(!file1.eof())
          {         
                file1.getline(str, 255);     
            if(check = strcmp(str, CNIC) == 0);
                 check=true;
          }
          file1.close();
          if(check)
          {
                 cout<<endl<<"CNIC number matched"<<endl;
                              }
          else
          {
                 cout<<endl<<"CNIC number did'nt match " ;
          }
    }
};

您的代码包含几个错误。

if 语句由 ; 终止,因此check将始终设置为 true

// the ';' terminates the if statement
if(check = strcmp(str, CNIC) == 0);
  // check will always be set to true
  check=true;

您需要删除分号,将代码更改为:

if(strcmp(str, CNIC) == 0) {
  check=true;
  // my guess would be that you really like to break the loop here
  break;
}

我还必须在 for 循环之后添加一个终止空字符CNIC,您需要在变量中为 16 个字符腾出空间,即char CNIC[16];为空字符腾出空间:

for(int i=0;i<15;i++)                    
{
  CNIC[i] = getche();
}
// add null character
CNIC[15] = 0;