有个C++程序错误,哪位大侠帮我解决一下,发个正确答案也好。最好改动少点。
设计一个学生类student,包括学生姓名、成绩,一个教师类teacher,包括教师姓名、职称。分别为两个类设计内联函数,试输出相关信息:学生姓名,成绩,教师姓名和职称。...
设计一个学生类student,包括学生姓名、成绩,一个教师类teacher,包括教师姓名、职称。分别为两个类设计内联函数,试输出相关信息:学生姓名,成绩,教师姓名和职称。
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class student
{
string m1;
int record;
public:
student(string e,int f)
{
m1=e;
record=f;
}
string getone(){return m1;}
int gettwo(){return record;}
};
class teacher
{
string m2;
string job;
public:
teacher(string g,string h)
{
m2=g;
job=h;
}
string getthree(){return m2;}
string getfour(){return job;}
};
vector<student> v1;
vector<teacher> v2;
void main()
{
string a;
string b;
string c;
int d;
while(1)
{
cout<<"请输入学生的姓名和成绩:"<<endl;
cin>>a>>d;
if(a=="")
break;
else
student p(a,d);
v1.push_back(p);
}
while(1)
{
cout<<"请输入教师的姓名和职称:"<<endl;
cin>>b>>c;
if(b=="")
break;
else
teacher q(b,c);
v2.push_back(q);
}
for(int i;i<v1.size();i++)
{
cout<<v1[i].getone()<<endl<<
v1[i].gettwo()<<endl;
}
for(int j;j<v2.size();j++)
{
cout<<v2[j].getthree()<<endl<<
v2[j].getfour()<<endl;
}
} 展开
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class student
{
string m1;
int record;
public:
student(string e,int f)
{
m1=e;
record=f;
}
string getone(){return m1;}
int gettwo(){return record;}
};
class teacher
{
string m2;
string job;
public:
teacher(string g,string h)
{
m2=g;
job=h;
}
string getthree(){return m2;}
string getfour(){return job;}
};
vector<student> v1;
vector<teacher> v2;
void main()
{
string a;
string b;
string c;
int d;
while(1)
{
cout<<"请输入学生的姓名和成绩:"<<endl;
cin>>a>>d;
if(a=="")
break;
else
student p(a,d);
v1.push_back(p);
}
while(1)
{
cout<<"请输入教师的姓名和职称:"<<endl;
cin>>b>>c;
if(b=="")
break;
else
teacher q(b,c);
v2.push_back(q);
}
for(int i;i<v1.size();i++)
{
cout<<v1[i].getone()<<endl<<
v1[i].gettwo()<<endl;
}
for(int j;j<v2.size();j++)
{
cout<<v2[j].getthree()<<endl<<
v2[j].getfour()<<endl;
}
} 展开
展开全部
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class student
{
string m1;
int record;
public:
student(string e,int f)
{
m1=e;
record=f;
}
string getone(){return m1;}
int gettwo(){return record;}
};
class teacher
{
string m2;
string job;
public:
teacher(string g,string h)
{
m2=g;
job=h;
}
string getthree(){return m2;}
string getfour(){return job;}
};
vector<student> v1;
vector<teacher> v2;
void main()
{
string a;
string b;
string c;
int d;
while(1)
{
cout<<"请输入学生的姓名和成绩:"<<endl;
cin>>a>>d;
student p(a, d);
if(a=="")
break;
v1.push_back(p);
if (!v1.empty())
break;
}
while(1)
{
cout<<"请输入教师的姓名和职称:"<<endl;
cin>>b>>c;
teacher q(b, c);
if(b=="")
break;
v2.push_back(q);
if (!v1.empty())
break;
}
for(int i = 0;i<v1.size();i++)
{
cout<<v1[i].getone()<<endl<<
v1[i].gettwo()<<endl;
}
for(int j = 0;j<v2.size();j++)
{
cout<<v2[j].getthree()<<endl<<
v2[j].getfour()<<endl;
}
}
只能输出一个……老师和学生。因为在2个while里面,重复定义了对象,我觉得吧,你最好另外定义2个给对象赋值的函数,这样就方便实现的多了。目前来说……改动最少的就是上面的例子……我的能力范围内吧……只能输入一个老师和学生……我提前声明……至于多次声明的话,不加点东西我改不了……
#include<string>
#include<vector>
using namespace std;
class student
{
string m1;
int record;
public:
student(string e,int f)
{
m1=e;
record=f;
}
string getone(){return m1;}
int gettwo(){return record;}
};
class teacher
{
string m2;
string job;
public:
teacher(string g,string h)
{
m2=g;
job=h;
}
string getthree(){return m2;}
string getfour(){return job;}
};
vector<student> v1;
vector<teacher> v2;
void main()
{
string a;
string b;
string c;
int d;
while(1)
{
cout<<"请输入学生的姓名和成绩:"<<endl;
cin>>a>>d;
student p(a, d);
if(a=="")
break;
v1.push_back(p);
if (!v1.empty())
break;
}
while(1)
{
cout<<"请输入教师的姓名和职称:"<<endl;
cin>>b>>c;
teacher q(b, c);
if(b=="")
break;
v2.push_back(q);
if (!v1.empty())
break;
}
for(int i = 0;i<v1.size();i++)
{
cout<<v1[i].getone()<<endl<<
v1[i].gettwo()<<endl;
}
for(int j = 0;j<v2.size();j++)
{
cout<<v2[j].getthree()<<endl<<
v2[j].getfour()<<endl;
}
}
只能输出一个……老师和学生。因为在2个while里面,重复定义了对象,我觉得吧,你最好另外定义2个给对象赋值的函数,这样就方便实现的多了。目前来说……改动最少的就是上面的例子……我的能力范围内吧……只能输入一个老师和学生……我提前声明……至于多次声明的话,不加点东西我改不了……
展开全部
#include<iostream.h> 头函数引用
编译的时候会提示你错误发生在哪一行,对照下改
编译的时候会提示你错误发生在哪一行,对照下改
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
输入别搞成无限循环。。不然怎么输出,还有那个else下要把两个语句括一起。。。不然他怎么找得到那个局部变量。。
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class student
{
string m1;
int record;
public:
student(string e,int f)
{
m1=e;
record=f;
}
string getone(){return m1;}
int gettwo(){return record;}
};
class teacher
{
string m2;
string job;
public:
teacher(string g,string h)
{
m2=g;
job=h;
}
string getthree(){return m2;}
string getfour(){return job;}
};
vector<student> v1;
vector<teacher> v2;
void main()
{
string a;
string b;
string c;
int d;
for(int k = 0; k < 1; k++)
{
cout<<"请输入学生的姓名和成绩:"<<endl;
cin>>a>>d;
if(a=="")
break;
else
{
student p(a,d);
v1.push_back(p);
}
}
for(int m = 0; m < 1; m++)
{
cout<<"请输入教师的姓名和职称:"<<endl;
cin>>b>>c;
if(b=="")
break;
else
{
teacher q(b,c);
v2.push_back(q);
}
}
for(int i = 0;i<v1.size();i++)
{
cout<<v1[i].getone()<<endl<<
v1[i].gettwo()<<endl;
}
for(int j = 0;j<v2.size();j++)
{
cout<<v2[j].getthree()<<endl<<
v2[j].getfour()<<endl;
}
}
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class student
{
string m1;
int record;
public:
student(string e,int f)
{
m1=e;
record=f;
}
string getone(){return m1;}
int gettwo(){return record;}
};
class teacher
{
string m2;
string job;
public:
teacher(string g,string h)
{
m2=g;
job=h;
}
string getthree(){return m2;}
string getfour(){return job;}
};
vector<student> v1;
vector<teacher> v2;
void main()
{
string a;
string b;
string c;
int d;
for(int k = 0; k < 1; k++)
{
cout<<"请输入学生的姓名和成绩:"<<endl;
cin>>a>>d;
if(a=="")
break;
else
{
student p(a,d);
v1.push_back(p);
}
}
for(int m = 0; m < 1; m++)
{
cout<<"请输入教师的姓名和职称:"<<endl;
cin>>b>>c;
if(b=="")
break;
else
{
teacher q(b,c);
v2.push_back(q);
}
}
for(int i = 0;i<v1.size();i++)
{
cout<<v1[i].getone()<<endl<<
v1[i].gettwo()<<endl;
}
for(int j = 0;j<v2.size();j++)
{
cout<<v2[j].getthree()<<endl<<
v2[j].getfour()<<endl;
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询