mercredi 15 juillet 2020

How to setup if statement option to make it working whenever I select 2?

In this code if-statement is giving me problem. Whenever I try to select option 2, it didn't give me the message of selected 2? How to make working this if statement working?

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

void hello(){
    int option;
    static const char * listing[] = {"Name", "Date of birth","ID card number"};
    FILE * fr3 = fopen("file.txt","r");

    if (fr3 == NULL) {
        perror("Unable to read text file.");
        exit(0);
    }
    for (option = 1; option <= sizeof(listing)/sizeof(char *); ++option)
       printf("%d. Your %s\n", option, listing[option-1]);  

    fputs("Select your choice to update: ", stdout);   
    if (scanf("%d", &option) == 1) {
        puts("selected 1");
        fclose(fr3);
        exit(0);
    }
    if (scanf("%d", &option) == 2) {
        puts("selected 2");
        fclose(fr3);
        exit(0);
    }
      fclose(fr3);
}
int main(){ hello(); }

Aucun commentaire:

Enregistrer un commentaire