求助,帮忙看一下哪里出错了,在输入第二组数据时会覆盖第一组
程序代码:
#define N 15 #include <stdio.h> #include <string.h> struct PERSON { int type; //联系人类型 char name[12]; //联系人姓名 char tel[12]; //联系人电话 char email[30]; //联系人Email }; struct PERSON person[N]; //int num = 0; //全局变量,当前联系人数 int menu() { int n; printf("\n\t************ MENU ************\n\n"); printf("\t\t 1. 查看所有联系人\n\n"); printf("\t\t 2. 添加联系人\n\n"); printf("\t\t 3. 修改联系人\n\n"); printf("\t\t 4. 删除联系人\n\n"); printf("\t\t 0. 退出\n"); printf("\n\t******************************\n\n"); printf("\tselect: "); scanf("%d", &n); if (n>4 || n<1) { printf("\nerror, select again\n"); //输入数据不合法,重新输入 } return n; } //显示所有联系人 void browse( struct PERSON person[],int number ) { int m,i,j = 0; printf("\t办公类--1 个人类--2 商务类--3\n"); printf("\t请选择联系人类型: "); scanf("%d",&m); for(i = 0;i <= number;i++) { if(person[i].type == m) { printf("\n\t\t姓名 电话\n"); printf("\t\t%-12s %-12s\n",person[i].name,person[i].tel); j++; } } if(j == 0) printf ("\n\t该目录下为空,请稍后再试!\n\n"); return ; } //排序 void sort(struct PERSON *person[],int number) { struct PERSON *temp; int j,k; for( j = 0;j < number-1;j++) for( k = j+1;k < number;k++) { if(strcmp(person[j]->name,person[k]->name)>0) { temp = person[j]; person[j] = person[k]; person[k] = temp; } } return ; } //添加联系人 void insert(struct PERSON *person,int number) { if(number == N) printf("\t记录已满,请先删除无用信息!\n"); else { printf("\n\t请输入姓名: "); scanf("%s",&person[number].name); printf("\t请输入电话号码: "); scanf("%s",&person[number].tel); printf("\t请输入Email地址: "); scanf("%s",&person[number].email); printf("\t请输入联系人类型(办公类--1 个人类--2 商务类--3): "); scanf("%d",&person[number].type); printf("\n\t添加联系人成功!\n"); } sort(&person,number); return ; } //修改联系人 void modify(struct PERSON *person,int number) { int i = 0; char nname[12]; printf("\t请输入要修改联系人的姓名: "); scanf("%s",&nname); while (i < number && strcmp(person[i].name,nname)) i++; if(!strcmp(person[i].name,nname)) //已查找到要修改的信息 { printf("\t您要修改的信息为:\n\n"); printf("\t\t姓名: "); printf("\t%-12s\n",person[i].name); printf("\t\t电话: "); printf("\t%-12s\n",person[i].tel); printf("\t\t邮箱: "); printf("\t%-12s\n",person[i].email); printf("\t\t类型: "); if(person[i].type == 1) printf("办公类\n"); else if(person[i].type == 2) printf("个人类\n"); else printf("商务类\n"); printf("\n\t请输入修改后的姓名: "); scanf("\n%s",&person[i].name); printf("\t请输入修改后的电话: "); scanf("%s",&person[i].tel); printf("\t请输入修改后的Email: "); scanf("%s",&person[i].email); printf("\t请输入修改后的联系人类型(办公类--1 个人类--2 商务类--3): "); scanf("%d",&person[i].type); sort(&person,number); printf("\n\t联系人修改成功!\n"); } else printf("\n\t未查找到相关信息,请稍后再试!\n"); } //删除联系人 void del( struct PERSON *person,int number ) { int control,i = 0; char nname[12]; printf("\t请输入要删除联系人的姓名: "); scanf("%s",&nname); while (i<number&&strcmp(person[i].name,nname)) i++; if(!strcmp(person[i].name,nname)) //已查找到要修改的信息 { printf("\t您要删除的信息为:\n\n"); printf("\t\t姓名: "); printf("\t%-12s\n",person[i].name); printf("\t\t电话: "); printf("\t%-12s\n",person[i].tel); printf("\t\t邮箱: "); printf("\t%-12s\n",person[i].email); printf("\t\t类型: "); if(person[i].type == 1) printf("办公类\n"); else if(person[i].type == 2) printf("个人类\n"); else printf("商务类\n"); printf("\n\t是否确认删除?(1--是,0--否): "); scanf("%d",&control); switch(control) { case 1:for(;i < number-1;i++) person[i] = person[i + 1]; printf("\n\t删除成功!\n");break; case 0:printf("\n\t联系人未删除!\n"); break; default:printf("\n\t选择有误,请稍后再试!\n"); break; } } else printf("\n\t抱歉!未找到相关信息! 请稍候再试!\n"); return ; } int main(int argc, char* argv[]) { printf("\t****************************************\n"); printf("\t********欢迎使用通讯录管理系统!*********\n"); printf("\t****************************************\n"); while(1) { int num = 0; //全局变量,当前联系人数 int n = menu(); if (n == 0) {printf("\t\n谢谢使用!再见!\n");break;} else { switch(n) { case 1:browse(person,num);break; case 2:insert(&person,num); num++;break; case 3:modify(&person,num);break; case 4:del(&person,num); num--;break; default:break; } } } return 0; }