vendredi 28 août 2020

How to make if-statement print required the result?

This code has one problem. The problem is in

Problem in if-statement

if(all_digits(to_find) && strlen(to_find) ==  13)

Whenever I enter 13 characters but not from the file then it gives me a message of else statement but printing Found. Hello World! also. Although Found. Hello World! is in if-statement. It should not be printed. How to make if-statement work correctly?

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int all_digits(char *s){
  for (; *s!=0; s++){
    if (!isdigit(*s)){
      return 0;
    }
  }
  return 1;
}

Another part of code

int main(){
  FILE * fr = fopen("file.csv", "r");

  char save[500], line[200],  to_find[50];
  int oneByOne = 0, numberOfFields=8;
  char *word = NULL;
  printf("Enter the ID card number: ");
  scanf("%s", to_find);

  if(all_digits(to_find) && strlen(to_find) ==  13){
    while(fgets(line, 200, fr)){
      word = strtok(line, "\n");
      strcpy(save, line);

      if (strstr(save, to_find)){
        char *wordone = strtok(save, ",");
        while (wordone != NULL){
          printf("Here are your details: %s\n", wordone);
          wordone = strtok(NULL, ",");
        }
      }
    }
    fclose(fr);

    printf("Found. Hello World!\n");
  }  
  else {
    printf("enter correclty\n");
  }
return 0;
}

Aucun commentaire:

Enregistrer un commentaire