c语言程序求助(不知错在那?)

yuusekitei 2011-11-06 06:42:29
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>

struct student
{
int num;
char name[20];
int age;
char sex[10];
char add[40];
struct student *next;
};

struct student *InHead()//定义头结点
{
struct student *p1;
p1=(struct student*)malloc(sizeof(struct student));
if(p1!=NULL)
{
p1->age=0;
p1->next=NULL;
}
else
printf("定义头结点失败!\n");

return p1;//返回头指针
}

struct student *AddInformation( struct student *head)//增加学生信息
{
struct student *p1,*p2,*p3;
p3=p1=head;
p2=(struct student*)malloc(sizeof(struct student));
if(p2!=NULL)
{
printf("请输入学生学号:\n");
scanf("%d",&p2->num);
printf("请输入学生姓名:\n");
scanf("%s",p2->name);
printf("请输入学生年龄:\n");
scanf("%d",&p2->age);
printf("请输入学生性别:\n");
scanf("%c",p2->sex);
printf("请输入学生地址:\n");
scanf("%s",p2->add);
while(p3->next!=NULL)
{
p3=p3->next;
}
p3->next=p2;
p2->next=NULL;
printf("增加学生信息成功!\n");
system("pause");
system("cls");
}
else
{
printf("分配存储空间失败!\n");
system("pause");
}

return p1; //返回头指针
}

void DisplayInformation(struct student *head)//显示所有学生信息
{
struct student *p1;
p1=head->next;
while(p1!=NULL)
{
printf("学号:%d\n",p1->num);
printf("姓名:%s\n",p1->name);
printf("年龄:%d\n",p1->age);
printf("性别:%c\n",p1->sex);
printf("地址:%s\n",p1->add);
p1=p1->next;
}
system("pause");
system("cls");
}

void DeleteInformation(struct student *p)//删除某个学生信息
{
struct student *p1;
p1=p->next;//删除p1所指向的学生信息
if (NULL!=p)
{
p->next=p->next->next;
free(p1);//释放存储空间,并且将改结点指针指向0
p1=0;
printf("删除学生信息成功!\n");
system("pause");
system("cls");
}

}

void UpdataInformation(struct student *p)//更新该节点的学生信息
{
struct student *p1;
p1=p->next;
if(p!=NULL)
{
p = p->next;
scanf("%d%s%c%d%s",&p->num,p->name,p->sex,&p->age,p->add);
printf("更新学生信息成功!\n");
}
else
printf("更新学生信息失败!\n");
system("pause");
system("cls");
}
void SearchStudentInformation(struct student *p)//查找某个学生的信息
{
struct student *p1;
if (NULL!=p)
{
p1=p->next;//p1指向的该节点为所需要查找的学生信息
printf("所查找的学生信息是:学号 %d 姓名 %s 年龄 %d 性别 %c 地址 %s\n",&p1->num,p1->name,&p1->age,&p1->sex,&p1->add);
}
else
{
printf("该学生不存在!\n");

}
}


void Print_1()
{
printf("************欢饮使用学生信息管理系统**********\n");
printf("***1.插入学生信息 **\n");
printf("***2.显示学生信息 **\n");
printf("***3.删除学生信息 **\n");
printf("***4.查找学生信息 **\n");
printf("***5.更新学生信息 **\n");
printf("***0.退出系统 **\n");
printf("**********************************************\n");
printf("请输入您的操作选择:(0--5)\n");


}

struct student * SearchStudentPoint(struct student *head,char p[])//根据姓名查找某个学生是否存在
{
struct student *p1,*p2;
if (NULL==head->next)
return 0;
else
{
p1=head->next;
p2=head;
while(p2->next!=NULL)
{

if(0==strcmp(p1->name,p))
return p2;//找到该学生并返回该生的结点指针

else
{
p2=p2->next;
p1=p1->next;
}
}
return 0;
}

}


int main(int nArgc,char *szArgv[])
{
char name[20];
char c=1;
struct student *head,*searchp;
head=InHead();//创建学生信息表头结点
while(c!='0')
{
Print_1();
fflush(stdin);
c=getchar();
switch(c)
{
case '1':
AddInformation(head);
break;
case '2':
DisplayInformation(head);
break;
case '3':
printf("请输入所要删除学生的姓名:\n");
scanf("%s",name);
searchp=SearchStudentPoint(head,name);
if (NULL==searchp)
printf("该学生不存在!\n");
else
DeleteInformation(searchp);
break;
case '4':
printf("请输入所要查找学生的姓名:");
scanf("%s",name);
searchp=SearchStudentPoint(head,name);
if (NULL!=searchp)
{
SearchStudentInformation(searchp);
system("pause");
system("cls");
}
else

{
printf("该学生不存在!\n");
system("pause");
system("cls");
}
break;
case '5':
printf("请输入所要更新学生的姓名:\n");
scanf("%s",name);
searchp=SearchStudentPoint(head,name);
if (NULL==searchp)
printf("该学生不存在!\n");
else
UpdataInformation(searchp);
system("pause");
system("cls");
break;

case '0':
return 0;
default:
printf("该选择不存在!\n");
system("pause");
system("cls");
break;
}

}

}
...全文
70 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuusekitei 2011-11-06
  • 打赏
  • 举报
回复
恩,好像是;我加了return c;
就没有警告了,谢谢了高手,非常感谢
ye0915715 2011-11-06
  • 打赏
  • 举报
回复
你看下cpp4第242行代码。
ye0915715 2011-11-06
  • 打赏
  • 举报
回复
main函数有一个分支没有返回值。
yuusekitei 2011-11-06
  • 打赏
  • 举报
回复
printf("性别:%c\n",p1->sex);//%s
,是这个问题,但是程序含有一个警告
c:\users\administrator\desktop\vci\cpp4.cpp(242) : warning C4715: 'main' : not all control paths return a value
不知道什么意思??
yuusekitei 2011-11-06
  • 打赏
  • 举报
回复
不是内存问题,好像是vc6.0有这么一个bug,我的老师好像说过是再stdin键盘缓存问题。求指点
Thirty 2011-11-06
  • 打赏
  • 举报
回复
printf("性别:%c\n",p1->sex);//%s

printf("请输入学生性别:\n");
scanf("%c",p2->sex);//%s
printf("请输入学生地址:\n");
scanf("%s",p2->add);
while(p3->next!=NULL)
ye0915715 2011-11-06
  • 打赏
  • 举报
回复
将那个%c改为%S?试试。
ye0915715 2011-11-06
  • 打赏
  • 举报
回复
在内存申请那加一断点,然后按F5,到了那个断点后F10,一步步看在哪里出错。
yuusekitei 2011-11-06
  • 打赏
  • 举报
回复
c:\users\administrator\desktop\vci\cpp4.cpp(242) : warning C4715: 'main' : not all control paths return a value
就是这个警告,不知错在哪?
yuusekitei 2011-11-06
  • 打赏
  • 举报
回复
不是吧,只有一个警告,现在的问题是再输入性别和地址的提示输入一起显示在屏幕上
printf("请输入学生性别:\n");
printf("请输入学生地址:\n");
一起执行了,,求高手啊
yuusekitei 2011-11-06
  • 打赏
  • 举报
回复
大哥,那句啊?解释清楚一下,我的水平很差的
ye0915715 2011-11-06
  • 打赏
  • 举报
回复
对不起,看错了。上面的回帖请忽略。是不是访问越界的问题呢?
你单步跟一下,在哪里弹出错误。
ye0915715 2011-11-06
  • 打赏
  • 举报
回复
将那句改为:p2>next=p3;
手机回帖真困难啊。
yuusekitei 2011-11-06
  • 打赏
  • 举报
回复
主要是再进行增加学生信息时,程序自动报错并且结束
求帮助
ye0915715 2011-11-06
  • 打赏
  • 举报
回复
什么现象呢?
另外,楼主可以使用typedef关键字将结构定以为类型,就不用在student前面加struct

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧