0
       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.

  • The error is i left an ampersand of scanf("%d",option); it should be scanf("%d",&option); another problem is the string... it should not be scanf("%s",&add.name); instead of scanf("%s",add.name); because it was a string, so cannot use ampersand in the scanf... – Leo Jie Hui Feb 28 '13 at 19:14

2 Answers2

1

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

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
  • Thanks for the answer...now i having another problem...i changed the code...but there's some problem...can you help me check? Here's the link... http://stackoverflow.com/questions/15122225/contact-manager-with-c-program-by-using-structure-different-code – Leo Jie Hui Feb 27 '13 at 20:56
0

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).

ouah
  • 142,963
  • 15
  • 272
  • 331