Contact manager with c program by using structure different code

こ雲淡風輕ζ 提交于 2020-01-17 01:38:04

问题


Now I having another problem with that, after I changed my coding as shown below, it still showing out the error. Inside the coding, there was no any red underline, therefore I can't find out where's the error. So any error of this coding?

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

void addcontact(struct contact list[100]);
void read(struct contact list[100]);

int main (void)
{
    struct contact list[100];
    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:addcontact(list);read(list);
        break;
    }

    getch();
}

void addcontact(struct contact list[100])
{
    char name[20],email[20];
    int hpnum,no;

    printf("\nContact Name: ");
    scanf("%s",list[no-1].name);
    fflush(stdin);
    printf("\nHandphone Number: ");
    scanf("%d",&list[no-1].hpnum);
    fflush(stdin);
    printf("\nE-mail: ");
    scanf("%s",list[no-1].email);
}

void read(struct contact list[100])
{
  FILE *f;
  f=fopen("contact.txt","w");
  fwrite(list,sizeof(list),100,f);
  fclose(f);
}

回答1:


First, fflush(stdin); is undefined error. should use fflush(stdout);

Second, in function addcontact(struct contact list[100]) variable no has not assigned any value and you are using in scanf() function with garbage value.

Third, in read() function fwrite(list,sizeof(list),100,f); is wrong it should be like.

fwrite(list, sizeof(struct contact), 100, f);

I am not sure but it looks You are intended to read more then one contacts So you also need a looping mechanism probably inside addcontact() function



来源:https://stackoverflow.com/questions/15122225/contact-manager-with-c-program-by-using-structure-different-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!