关于C++的派生类的题目,求助~~!
在SWITCH语句那里出了很多问题#include<iostream.h>classStudents{charname[80];charsex[5],job[10];in...
在SWITCH语句那里出了很多问题
#include<iostream.h>
class Students{
char name[80];
char sex[5],job[10];
int age;
public:
Students(char *p,char *q,char *r,int n)
{
strcpy(name,p);
strcpy(sex,p);
strcpy(job,r);
age=n;
}
void display1();
void display2();
};
void Students:: display1()
{
char message1[5];
cout<<"请输入您所要的信息:";
cin>>message1;
switch(message1)
{
case "姓名:":cout<<name<<endl;break;
case "性别:":cout<<sex<<endl;break;
case "职业:":cout<<job<<endl;break;
case "年龄:":cout<<age<<endl;break;
default:cout<<"输入错误"<<endl;break;
}
}
void Students:: display2()
{
cout<<"姓名:"<<name<<" "
<<"性别:"<<sex<<" "
<<"职业:"<<job<<" "
<<"年龄:"<<age<<endl;
}
class Messages:public Students
{
char school[20],major[10];
int grade;
public:
Messages(char *p,char *q,char *r,int n,char *s char *t,int m):
Students(*p,*q,*r,n),school(*s),major(*t),grade(m)
{
strcpy(school,s);
strcpy(major,t);
grade=m;
}
void display3();
void display4();
};
void Messages::display3()
{
Students::display1();
char message2[20];
cout<<"请输入您所要的信息:";
cin>>message2;
switch(message2)
{
case "学校:":cout<<school<<endl;break;
case "专业:":cout<<major<<endl;break;
case "成绩:":cout<<grade<<endl;break;
default:cout<<"输入错误"<<endl;break;
}
}
void Messages::display4()
{
cout<<"学校:"<<school<<" "
<<"专业:"<<major<<" "
<<"成绩:"<<grade<<endl;
}
void main()
{
Messages a("张三","男","学生",19,"**大学","专业",80);
a.display3;
a.display4;
}
请帮忙改一下~~~~然后说明下我错在了哪里~~~~~~~~谢谢~~~感激不尽~~ 展开
#include<iostream.h>
class Students{
char name[80];
char sex[5],job[10];
int age;
public:
Students(char *p,char *q,char *r,int n)
{
strcpy(name,p);
strcpy(sex,p);
strcpy(job,r);
age=n;
}
void display1();
void display2();
};
void Students:: display1()
{
char message1[5];
cout<<"请输入您所要的信息:";
cin>>message1;
switch(message1)
{
case "姓名:":cout<<name<<endl;break;
case "性别:":cout<<sex<<endl;break;
case "职业:":cout<<job<<endl;break;
case "年龄:":cout<<age<<endl;break;
default:cout<<"输入错误"<<endl;break;
}
}
void Students:: display2()
{
cout<<"姓名:"<<name<<" "
<<"性别:"<<sex<<" "
<<"职业:"<<job<<" "
<<"年龄:"<<age<<endl;
}
class Messages:public Students
{
char school[20],major[10];
int grade;
public:
Messages(char *p,char *q,char *r,int n,char *s char *t,int m):
Students(*p,*q,*r,n),school(*s),major(*t),grade(m)
{
strcpy(school,s);
strcpy(major,t);
grade=m;
}
void display3();
void display4();
};
void Messages::display3()
{
Students::display1();
char message2[20];
cout<<"请输入您所要的信息:";
cin>>message2;
switch(message2)
{
case "学校:":cout<<school<<endl;break;
case "专业:":cout<<major<<endl;break;
case "成绩:":cout<<grade<<endl;break;
default:cout<<"输入错误"<<endl;break;
}
}
void Messages::display4()
{
cout<<"学校:"<<school<<" "
<<"专业:"<<major<<" "
<<"成绩:"<<grade<<endl;
}
void main()
{
Messages a("张三","男","学生",19,"**大学","专业",80);
a.display3;
a.display4;
}
请帮忙改一下~~~~然后说明下我错在了哪里~~~~~~~~谢谢~~~感激不尽~~ 展开
3个回答
展开全部
#include<iostream.h>
#include<string.h>/*首先少了一个头文件*/
class Students{
char name[80];
char sex[5],job[10];
int age;
public:
Students(char *p,char *q,char *r,int n)
{
strcpy(name,p);
strcpy(sex,p);
strcpy(job,r);
age=n;
}
void display1();
void display2();
};
void Students:: display1()
{
char message1;
cout<<"请输入您所要的信息:";
cin>>message1;
switch(message1)
{
case 'n':cout<<name<<endl;break;
case 's':cout<<sex<<endl;break;
case 'j':cout<<job<<endl;break;
case 'a':cout<<age<<endl;break;
default:cout<<"输入错误"<<endl;break;
}
}
void Students:: display2()
{
cout<<"姓名:"<<name<<" "
<<"性别:"<<sex<<" "
<<"职业:"<<job<<" "
<<"年龄:"<<age<<endl;
}
class Messages:public Students
{
char school[20],major[10];
int grade;
public:
Messages(char *p,char *q,char *r,int n,char *s,char *t,int m):Students(p,q,r,n)
{
strcpy(school,s);
strcpy(major,t);
grade=m;
}
/* Messages(char *p,char *q,char *r,int n,char *s char *t,int m): Students(p,q,r,n)
{
strcpy(school,s);
strcpy(major,t);
grade=m;
}*/
void display3();
void display4();
};
/*构造函数出错,上面是我改正过的*/
void Messages::display3()
{
Students::display1();
char message2;
cout<<"请输入您所要的信息:";
cin>>message2;
switch(message2)
{
case 'x':cout<<school<<endl;break;
case 'z':cout<<major<<endl;break;
case 'c':cout<<grade<<endl;break;
default:cout<<"输入错误"<<endl;break;
}
}
void Messages::display4()
{
cout<<"学校:"<<school<<" "
<<"专业:"<<major<<" "
<<"成绩:"<<grade<<endl;
}
void main()
{
Messages a("张三","男","学生",19,"**大学","专业",80);
a.display3();
a.display4();/*少了括号*/
}
/*最主要的错误是因为switch不能传字符串进行比较,只有字符和整形数据可以,现在已经调试通过,具体还有什么再问我,一起学习啊,呵呵*/
#include<string.h>/*首先少了一个头文件*/
class Students{
char name[80];
char sex[5],job[10];
int age;
public:
Students(char *p,char *q,char *r,int n)
{
strcpy(name,p);
strcpy(sex,p);
strcpy(job,r);
age=n;
}
void display1();
void display2();
};
void Students:: display1()
{
char message1;
cout<<"请输入您所要的信息:";
cin>>message1;
switch(message1)
{
case 'n':cout<<name<<endl;break;
case 's':cout<<sex<<endl;break;
case 'j':cout<<job<<endl;break;
case 'a':cout<<age<<endl;break;
default:cout<<"输入错误"<<endl;break;
}
}
void Students:: display2()
{
cout<<"姓名:"<<name<<" "
<<"性别:"<<sex<<" "
<<"职业:"<<job<<" "
<<"年龄:"<<age<<endl;
}
class Messages:public Students
{
char school[20],major[10];
int grade;
public:
Messages(char *p,char *q,char *r,int n,char *s,char *t,int m):Students(p,q,r,n)
{
strcpy(school,s);
strcpy(major,t);
grade=m;
}
/* Messages(char *p,char *q,char *r,int n,char *s char *t,int m): Students(p,q,r,n)
{
strcpy(school,s);
strcpy(major,t);
grade=m;
}*/
void display3();
void display4();
};
/*构造函数出错,上面是我改正过的*/
void Messages::display3()
{
Students::display1();
char message2;
cout<<"请输入您所要的信息:";
cin>>message2;
switch(message2)
{
case 'x':cout<<school<<endl;break;
case 'z':cout<<major<<endl;break;
case 'c':cout<<grade<<endl;break;
default:cout<<"输入错误"<<endl;break;
}
}
void Messages::display4()
{
cout<<"学校:"<<school<<" "
<<"专业:"<<major<<" "
<<"成绩:"<<grade<<endl;
}
void main()
{
Messages a("张三","男","学生",19,"**大学","专业",80);
a.display3();
a.display4();/*少了括号*/
}
/*最主要的错误是因为switch不能传字符串进行比较,只有字符和整形数据可以,现在已经调试通过,具体还有什么再问我,一起学习啊,呵呵*/
展开全部
switch中的条件表达式只能为整型数据,或可转换成整型的数据。你可以cout<<"请选择:1.学校,2.专业,3.成绩";然后在case中写 1,2,3
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
switch语句括号里面一定要是int类型,用if else判断语句加上strcmp函数就行了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询