the first time enter the "while loop" and the second no! Why?

Discussion in 'C' started by brack, Aug 31, 2010.

  1. brack

    brack New Member

    Joined:
    Aug 31, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    When i type first time 1 the printf appeared, but the second time i typed 1 there where no printf...can anyone help me?

    Code:
    #include
    #include

    struct Info{
    char name[81];
    int age;
    char email[81];
    int c;
    };

    int main(void){
    FILE* infile;
    struct Info info;

    infile = fopen("my_file.txt","r+"); // pos: r, w, a, r+, w+, a+
    if(infile ==NULL){
    printf("File does not exist.\n");
    exit(1);
    }else{
    printf("Please type 1 to see the statistics.\n");
    printf("Please type 2 to add a new contact.\n");
    printf("Plesase type 0 to exit.\n");
    scanf("%d", &info.c);
    while(1){


    if(info.c == 1){
    while(fscanf(infile,"%s %d %s", info.name, &info.age, info.email) != EOF){
    printf("\tName: %s\n", info.name);
    printf("\tAge: %d\n", info.age);
    printf("\tE-mail: %s\n\n", info.email);
    }
    }else if(info.c == 2){
    printf("New name:\n");
    scanf("%s", info.name);
    printf("New age:\n");
    scanf("%d", &info.age);
    printf("New e-mail:\n");
    scanf("%s", info.email);
    fprintf(infile,"%s %d %s", info.name, info.age, info.email);

    fclose(infile);
    infile = fopen("my_file.txt","r+");
    }else{
    exit(0);
    }
    printf("Please type 1 to see the statistics.\n");
    printf("Please type 2 to add a new contact.\n");
    printf("Plesase type 0 to exit.\n");
    scanf("%d", &info.c);
    }
    }
    fclose(infile);
    return 0;
    }
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice