-5

It wasn't work properly as I want it to work .. According to my thinking,code which is given below is always true i don't know why but if-condition always true and number got matched .. Variable named as CNIC was globally declared like this(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 " ;
          }
    }
};
Borgleader
  • 15,349
  • 5
  • 42
  • 59
  • 4
    Please change your title to something relevant to the problem you're having. "My code is not working please help" is not a useful title. – Borgleader Jan 26 '15 at 13:25
  • 1
    Watch those semicolons. Carefully. (And get yourself an editor that indents for you.) Also, [`eof` is wrong](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – molbdnilo Jan 26 '15 at 13:29
  • 1
    Add paranthesis and remove the `;` which end the statement: `if((check = strcmp(str, CNIC) == 0)) check=true;` – Cyclonecode Jan 26 '15 at 13:29
  • Guys,i'm just a fresher.Need some time to be like you.By the way thanks for helping – Ayub Îdréès Jan 26 '15 at 13:40

1 Answers1

0

You code contains a couple of errors.

The if statement is terminated by ; so check will always be set to true

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

You need to remove the semicolon, change your code to:

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

I also must add a terminating null character to CNIC after your for loop, you will need to make room for 16 characters in the variable i.e char CNIC[16]; to make room for the null character:

for(int i=0;i<15;i++)                    
{
  CNIC[i] = getche();
}
// add null character
CNIC[15] = 0;
Cyclonecode
  • 26,179
  • 11
  • 66
  • 86
  • I changed the code as you said but else-condition is now working again and again .. – Ayub Îdréès Jan 26 '15 at 15:02
  • @AyubÎdréès - I also think you should add a null character to `CNIC`. Look at my edited answer above. – Cyclonecode Jan 26 '15 at 15:15
  • In file this line is written (PTI 1233542 1 Time of vote : 17:56:07) .. I want to search that 1233542 is already present or not,what shall i do to do that !! Do i split that data first then search for that number or is there any easy approach to tackle that problem .. – Ayub Îdréès Jan 26 '15 at 16:52
  • @AyubÎdréès - Start by accepting the answer if it solved you problem. Then you should create a new question since this don't seem to have anything to do with your previous question. – Cyclonecode Jan 26 '15 at 20:35