郁闷!
题目:编写通讯录管理程序
每个人有姓名、电话、移动电话、邮编和通讯地址信息
可以存储50个人的信息
要求1:提供录入、删除、修改和查询的功能
2。删除、修改和查询的时候提示用户输入姓名,找到对应位置后进行相关操作
我写的程序如下:但是有问题,每当输入一个名字时(执行函数语句),总是显示"无此人";琢磨了半天,不解?郁闷!望大虾们指点(若有好的方法,不要保留哦!)!不甚感激!!!
# include <stdio.h>
# include <string.h>
# define N 4 /* 可依据需要决定 */
int showmenu() /* 打印菜单 */
{ int i,j;
for(i=0;i<=24;i++)
printf("\n");
do{
printf( "************************************\n ");
printf( "1---录入 \n ");
printf( "2---删除 \n ");
printf( "3---修改 \n ");
printf( "4---查询 \n ");
printf( "5---退出 \n ");
printf( "*************************************\n");
printf( "please choose 1,2 ,3, or 5: \n ");
scanf("%d",&j);
}while(j>5||j<1);
return (j);
}
struct student
{char name[10];
long phone;
long mobile;
long mail;
char addr[30];
};
void adding(struct student stu[]) /*录入*/
{
int i ;
for(i=0;i<N;i++)
{
printf( "plaese input name \n ");
fflush(stdin);
gets(stu->name);
printf( "plaese input phone number: \n");
scanf( "%ld",&stu->phone);
printf( "plaese input mobile number: \n ");
scanf( "%d",&stu->mobile );
printf( "plaese input mail number: \n ");
scanf( "%ld",&stu->mail);
printf( "plaese input adderess: \n ");
fflush(stdin);
gets(stu->addr);
}
}
int dismiss(struct student stu[]) /*删除*/
{ char str[10];int i;
printf("please input his(her) name:");
fflush(stdin);
gets(str);
for(i=0;i<N-1;i++)
{ if (strcmp(str,stu[i].name)==0)
{strcpy(stu[i].name,stu[i+1].name);
stu[i].phone=stu[i+1].phone;
stu[i].mail=stu[i+1].mail;
strcpy( stu[i].addr,stu[i+1].addr);
}
else
return (-1);
}
if(strcmp(str,stu[N-1].name)==0)
{strcpy(stu[N-1].name, " ");
stu[i].phone=0;
stu[i].mail=0;
strcpy( stu[N-1].addr," ");
}
;
return 0;
}
int modify(struct student stu[]) /*修改*/
{ int i; char str[10];
printf( "please input his(her) name: ");
fflush(stdin);
gets(str);
for(i=0;i<N;i++)
{ if (strcmp(str,stu[i].name)==0)
{
printf( "plaese input a new phone number: \n ");
scanf( "%d",&stu[i].phone);
printf( "plaese input a new mobile number: \n ");
scanf( "%d",&stu[i].mobile );
printf( "plaese input a new mail number: \n ");
scanf( "%d",&stu[i].mail );
printf( "plaese input a new adderess: \n ");
fflush(stdin);
gets(stu[i].addr);
}
else
return (-1);
}
return 0;
}
int search(struct student stu[]) /*查询*/
{ int i; char str[10];
printf("please input his(her) name:");
fflush(stdin);
gets(str);
for(i=0;i<N;i++)
{ if (strcmp(str,stu[i].name)==0)
printf("name:%s\tphone:%ld\tmobile:%ld\tmail:%ld\taddress:%s\n",stu[i].name,stu[i].phone,stu[i].mobile,stu[i].mail,stu[i].addr);
else return -1;
}
return 0;
}
main()
{int x,y;struct student stu[N];
struct student *p;
p=stu;
do
{x=showmenu();
switch (x)
{case 1: adding(p);break;
case 2: y=dismiss(p);
if(y==-1)
printf("无此人\n");
break;
case 3: y=modify(p) ;
if(y==-1)
printf("无此人\n");
break;
case 4: y=search(p);
if(y==-1)
printf("无此人\n");
break;
}
}while(x!=5);
}