Contact manager with c program by using structure

2019-08-30 10:10发布

       struct contact
       {
       char name[20],email[20];
       int hpnum;
       }add;
   int option;

       int main (void)
       {
    system("cls");
    printf("==========Welcome to Jeffery's Contact System Management==========\n");
    printf("\t\t\tContact System Main Menu\n");
    printf("[1] Create a New Contact\n");
    printf("[2] Modified Existing Contact\n");
    printf("[3] Delete Existing Contact\n");
    printf("[4] Search Existing Contact\n");
    printf("[5] Exit\n");
    printf("Please enter one of your option.\n");
    scanf("%d",option);

    switch(option)
    {
        //add new contact
        case 1:
        printf("Contact Name: ");
        scanf("%s",&add.name);
        printf("\nHandphone Number: ");
        scanf("%d",&add.hpnum);
        printf("\nE-mail: ");
        scanf("%s",add.email);
        break;
    }

getch();
    }

What's the problem of this coding? I can run it, but once choose no. 1, there haven an error. If my coding is wrong, please inform me. I still a beginner, hope you all can help me in this.

2条回答
干净又极端
2楼-- · 2019-08-30 10:51

You forgot to add ampersand & before option variable.

 scanf("%d",option);

should be

 scanf("%d",&option);

option is an int, so here need & in scanf.

second: ampersand need to remove before add.name

 scanf("%s",&add.name)

should be

 scanf("%s",add.name)

add.name is a char array we don't need & with %s

查看更多
我命由我不由天
3楼-- · 2019-08-30 10:53

Add a fflush(stdout); between these two statements:

    printf("Contact Name: ");
    scanf("%s",&add.name); 

Same with your other printf statements that don't end with a \n character.

And for information s conversion specifier expects a pointer to char so don't use the & operator and just call scanf("%s", add.name).

查看更多
登录 后发表回答