运行总是学生不存在 该怎么改 TAT求大神帮忙
#include<stdio.h>#include<string.h>
struct Student
{
char ID[20];
char Name[20];
char Phone[16];
};
/*声明学生数组及学生数量*/
struct Student students[1000];
int num=0;
/*通过学号返回数组下标*/
int Student_SearchByIndex(char id[])
{
int i;
for (i=0;i<num;i++)
{
if (strcmp(students[i].ID,id)==0)
{
return i;
}
}
return -1;
}
/*通过姓名返回数组下标*/
int Student_SearchByName(char name[])
{
int i;
for (i=0;i<num;i++)
{
if (strcmp(students[i].Name,name)==0)
{
return i;
}
}
return -1;
}
/*显示单条学生记录*/
void Student_DisplaySingle(int index)
{
printf("%10s%10s%s\n","学号","姓名","电话");
printf("*************************************\n");
printf("%10s%10s%s\n",students[index].ID,students[index].Name,students[index].Phone);fflush(stdin);
}
/*插入学生信息*/
void Student_Insert()
{
while(1)
{
printf("请输入学号:");
scanf("%s",&students[num].ID);
getchar();fflush(stdin);
printf("请输入姓名:");
scanf("%s",&students[num].Name);
getchar();fflush(stdin);
printf("请输入号码:");
scanf("%s",&students[num].Phone);
getchar();fflush(stdin);
printf("是否继续?(y/n)");
if (getchar()=='n')
{
break;
}
}
}
/*修改学生信息*/
void Student_Modify()
{
while(1)
{
char id[20];
int index;
printf("请输入要修改的学生的学号:");
scanf("%s",&id);
getchar();
index=Student_SearchByIndex(id);
if (index==-1)
{
printf("学生不存在!\n");
}
else
{
printf("你要修改的学生号码为:\n");
Student_DisplaySingle(index);
printf("** 请输入新值**\n");
printf("请输入学号:");
scanf("%s",&students[index].ID);
getchar();
printf("请输入姓名:");
scanf("%s",&students[index].Name);
getchar();
printf("请输入号码:");
scanf("%f",&students[index].Phone);
getchar();
}
printf("是否继续?(y/n)");
if (getchar()=='n')
{
break;
}
}
}
/*删除学生信息*/
void Student_Delete()
{
int i;
while(1)
{
char id[20];
int index;
printf("请输入要删除的学生的学号:");
scanf("%s",&id);
getchar();
index=Student_SearchByIndex(id);
if (index==-1)
{
printf("学生不存在!\n");
}
else
{
printf("你要删除的学生号码为:\n");
Student_DisplaySingle(index);
printf("是否真的要删除?(y/n)");
if (getchar()=='y')
{
for (i=index;i<num-1;i++)
{
students[i]=students[i+1];//把后边的对象都向前移动
}
num--;
}
getchar();
}
printf("是否继续?(y/n)");
if (getchar()=='n')
{
break;
}
}
}
/*按姓名*/
void Student_Search()
{
while(1)
{
char name[20];
int index;
printf("请输入要查询的学生的姓名:");
scanf("%s",&name);
getchar();
index=Student_SearchByName(name);
if (index==-1)
{
printf("学生不存在!\n");
}
else
{
printf("你要查询的学生号码为:\n");
Student_DisplaySingle(index);
}
printf("是否继续?(y/n)");
if (getchar()=='n')
{
break;
}
}
}
/*显示电话信息*/
void Student_Display()
{
int i;
printf("%10s%10s%s\n","学号","姓名","号码");
printf("\n");
for (i=0;i<num;i++)
{
printf("%10s%10s%s\n",students[i].ID,students[i].Name,students[i].Phone);
}
}
void main()
{
int n;
while(1)
{
printf("\n******欢迎来到学生手机号码管理系统*******\n");
printf(" *******1.录入学生号码*****\n");
printf(" *******2.修改学生号码*****\n");
printf(" *******3.删除学生号码*****\n");
printf(" *******4.按姓名查询学生号码*****\n");
printf(" *******5.退出********\n");
printf("请选择(1-5):");
scanf("%d",&n);
getchar();
switch(n)
{
case 1: Student_Insert();break;
case 2:Student_Modify(); break;
case 3:Student_Delete(); break;
case 4:Student_Search(); break;
case 5:break;
}
}
}