编译没问题,运行时出现连接错误,求帮助

Skeram_Huang 2018-06-09 10:52:11
#include<iostream.h>
#include<string>
const int N=5;
const int M=2;
class Data
{
protected:
char *name;
char sex;
int age;
public:
void Set(char *n,char s,int a){name=n,sex=s,age=a;}
char GetName()
{return *name;}
char GetSex()
{return sex;}
int GetAge()
{return age;}
void Name()
{
char *n;
cout<<"请输入修改后的名字:",cin>>*n;
name=n;
}
void Sex()
{
char s;
cout<<"请输入修改后的性别:",cin>>s;
sex=s;
}
void Age()
{
int a;
cout<<"请输入修改后的年龄:",cin>>a;
age=a;
}
};
class Patient:virtual public Data
{
protected:
char*illness;
int number;
int day;
public:
void Input();
void OutPut();
void Illness()
{
char*n;
cout<<"请输入修改后的疾病信息:",cin>>*n;
illness=n;
}
void Number()
{
int a;
cout<<"请输入修改后的编号:",cin>>a;
number=a;
}
void Day()
{
int a;
cout<<"请输入修改后的入院天数:",cin>>a;
day=a;
}
};
class Doctor:public Data
{
protected:
char*function;
char*title;
int number;
char*control;
public:
void Input();
void Output();
void Function()
{
char*n;
cout<<"请输入修改后的隶属科室:",cin>>*n;
function=n;
}
void Title()
{
char*n;
cout<<"请输入修改后的职称:",cin>>*n;
title=n;
}
void Control()
{
char*n;
cout<<"请输入修改后的分管病房:",cin>>*n;
control=n;
}
};
void Patient::Input()
{
cout<<"请输入患者信息:"<<endl;
cout<<"姓名:",cin>>*name;
cout<<endl<<"性别:",cin>>sex;
cout<<endl<<"年龄:",cin>>age;
cout<<endl<<"疾病信息:",cin>>*illness;
cout<<endl<<"患者编号:",cin>>number;
cout<<endl<<"入院天数:",cin>>day;
}
void Patient::OutPut()
{
char *n,s;
int a;
cout<<"姓名:"<<*n<<endl;
cout<<"性别:"<<s<<endl;
cout<<"年龄:"<<a<<endl;
Set(n,s,a);
cout<<"疾病信息:"<<illness<<endl;
cout<<"编号:"<<number<<endl;
cout<<"入院天数:"<<day<<endl;
}
void Doctor::Input()
{
cout<<"请输入医生信息:"<<endl;
cout<<"姓名:",cin>>*name;
cout<<endl<<"性别:",cin>>sex;
cout<<endl<<"年龄:",cin>>age;
cout<<endl<<"隶属科室:",cin>>*function;
cout<<endl<<"职称:",cin>>*title;
cout<<endl<<"医生编号:",cin>>number;
cout<<endl<<"分管病房号:",cin>>*control;
}
void Doctor::Output()
{ cout<<"姓名:"<<*name<<endl;
cout<<"性别:"<<sex<<endl;
cout<<"年龄:"<<age<<endl;
cout<<"隶属科室:"<<*function<<endl;
cout<<"职称:"<<*title<<endl;
cout<<"编号:"<<number<<endl;
cout<<"分管病房号:"<<*control<<endl;
}
class Interface
{
Patient pa[N];
int nump;
Doctor oc[M];
int numa;
public:
Interface(){nump=0,numa=0;}
void display();
void run();
void Intput();
void Output();
void Delete();
void Modify();
};
void Interface::display()
{
cout<<endl<<"0、退出系统 1、输入信息 2、修改信息"<<endl;
cout<<"3、删除信息 4、浏览信息 "<<endl<<endl;
}
void Interface::run()
{
int choice;
cout<<endl<<"欢迎使用病房管理系统!"<<endl;
do
{
display();
cout<<"请输入执行功能:",cin>>choice;
switch(choice)
{
case 0:
cout<<"欢迎再次使用!"<<endl;
break;
case 1:
Intput();
break;
case 2:
Modify();
break;
case 3:
Delete();
break;
case 4:
Output();
break;
}
}while(choice);
}
void Interface::Intput()
{
int type;
cout<<"你要输入的人员类型是:"<<endl;
cout<<"1-患者信息 2-医生信息 3-退出录入"<<endl;
do
{
cout<<"输入操作指令:";
cin>>type;
if(type==1)
{
if(nump==N)
{
cout<<"患者人数已满,无法执行此次操作!"<<endl;
}
else
{
pa[nump].Input();
nump++;
}
}
else if(type==2)
{
if(numa==M)
{
cout<<"医生人数已满,无法执行此次操作!"<<endl;
}
else
{
oc[numa].Input();
numa++;
}
}
}while(type!=3);
}
void Interface::Modify()
{
int type1,type2;
int i;
cout<<"你将要修改的人员类型及其他操作:1-病人信息 2-医生信息 3-退出修改"<<endl;
do
{
cout<<"输入操作指令:",cin>>type1;
if(type1==1)
{
cout<<"请输入将要修改的患者编号:",cin>>i;
cout<<"1-姓名 2-性别 3-年龄 4-疾病信息 5-入院天数 6-返回上一级"<<endl;
do
{
cout<<"输入操作指令:",cin>>type2;
if(type2==1){pa[i-1].Name();}
else if(type2==2){pa[i-1].Sex();}
else if(type2==3){pa[i-1].Age();}
else if(type2==4){pa[i-1].Illness();}
else if(type2==5){pa[i-1].Day();}
}while(type2!=6);
}
if(type1==2)
{
cout<<"请输入将要修改的患者编号:",cin>>i;
cout<<"1-姓名 2-性别 3-年龄 4-隶属科室 5-职称 6-分管病房 7-返回上一级"<<endl;
do
{
cout<<"输入操作指令:",cin>>type2;
if(type2==1){oc[i-1].Name();}
else if(type2==2){oc[i-1].Sex();}
else if(type2==3){oc[i-1].Age();}
else if(type2==4){oc[i-1].Function();}
else if(type2==5){oc[i-1].Title();}
else if(type2==6){oc[i-1].Control();}
}while(type2!=7);
}
}while(type1!=3);
}
void Interface::Delete()
{
cout<<"你将要删除的人员类型及其他操作:1-患者信息 2-医生信息 3-退出删除"<<endl;
int i,type;
cin>>type;
do
{
cout<<"输入操作指令:",cin>>type;
if(type==1)
{
cout<<"输入删除患者的编号:",cin>>i;
pa[i-1].~Patient();
for(int a=0;a<N-1;a++)
{
if(a<i-1) break;
if(a=i-1) pa[a]=pa[a+1];
}
}
if(type==2)
{
cout<<"输入删除医生者的编号:",cin>>i;
oc[i-1].~Doctor();
for(int a=0;a<M-1;a++)
{
if(a<i-1) break;
if(a=i-1) oc[a]=oc[a+1];
}
}
}while(type!=3);
}
void Interface::Output()
{
int i,type1,type2;
cout<<"你将要浏览的人员类型及其他操作:1-患者信息 2-医生信息 3-退出浏览"<<endl;
do
{
cout<<"输入操作指令:",cin>>type1;
if(type1==1)
{
cout<<"1-全部浏览 2-选择浏览 3-返回上一级",cin>>type2;
do
{
if(type2==1)
{
for(int a=0;a<N;a++)
{
pa[a].OutPut();
}
}
if(type2==2)
{
cout<<"输入要浏览的患者编号:",cin>>i;
pa[i-1].OutPut();
}
}while(type2!=3);
}
if(type1==2)
{
cout<<"1-全部浏览 2-选择浏览 3-返回上一级",cin>>type2;
do
{
if(type2==1)
{
for(int a=0;a<M;a++)
{
oc[a].Output();
}
}
if(type2==2)
{
cout<<"输入要浏览的医生编号:",cin>>i;
oc[i-1].Output();
}
}while(type2!=3);
}
}while(type1!=3);
}
int main()
{
Interface Hospital;
Hospital.run();
return 0;
}

...全文
1325 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
冷风1023 2018-06-09
  • 打赏
  • 举报
回复
所有的char *的地址空间在哪?
AlbertS 2018-06-09
  • 打赏
  • 举报
回复
把出现的错误代码贴出来吧,这种错误应该比较好发现
自信男孩 2018-06-09
  • 打赏
  • 举报
回复
void Function()
    {
        char*n;
        cout<<"请输入修改后的隶属科室:",cin>>*n;
        function=n;
    }
    void Title()
    {
        char*n;
        cout<<"请输入修改后的职称:",cin>>*n;
        title=n;
    }
    void Control()
    {
        char*n;
        cout<<"请输入修改后的分管病房:",cin>>*n;
        control=n;
    }
代码出现异常,异常退出应该跟这几个的cin有关。n是指针,并且是野指针,若没给其分配空间就直接使用,可能会导致段错误; 要么就不要定义指针,直接定义一个普通变量 建议检查其他地方,看一下是不是也有类似这样的错误用法
paschen 2018-06-09
  • 打赏
  • 举报
回复
很多地方使用了未初始的指针,如char*n; 等 if (a = i - 1) 应该改为:if (a == i - 1)
Skeram_Huang 2018-06-09
  • 打赏
  • 举报
回复
总是已停止工作,找不出错的地方,希望各位大佬帮帮我
Skeram_Huang 2018-06-09
  • 打赏
  • 举报
回复
我觉得也是赋值的问题

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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