c++程序代码编译没问题但是不能运行
#include<iostream>usingnamespacestd;classStudent{char*number;char*name;intscore;publi...
#include<iostream>
using namespace std;
class Student{
char*number;
char*name;
int score;
public:
Student(){
number="kong";
name="kong";
score=0;
}
void setclub();
void display();
};
void Student::setclub()
{
cin>>number>>name>>score;
}
void Student::display()
{
cout<<number<<endl<<name<<endl<<score<<endl;
}
int main()
{
Student s1,s2,s3;
s1.setclub();
s2.setclub();
s3.setclub();
s1.display();
s2.display();
s3.display();
system("pause");
return 0;
} 展开
using namespace std;
class Student{
char*number;
char*name;
int score;
public:
Student(){
number="kong";
name="kong";
score=0;
}
void setclub();
void display();
};
void Student::setclub()
{
cin>>number>>name>>score;
}
void Student::display()
{
cout<<number<<endl<<name<<endl<<score<<endl;
}
int main()
{
Student s1,s2,s3;
s1.setclub();
s2.setclub();
s3.setclub();
s1.display();
s2.display();
s3.display();
system("pause");
return 0;
} 展开
2个回答
展开全部
建议用到字符串使用string类,可以避免原来代码中字符串赋值时的越界,代码如下 :
#include<iostream>
#include<string>
using namespace std;
class Student
{
string number;
string name;
int score;
public:
Student()
{
number="kong";
name="kong";
score=0;
}
void setclub();
void display();
};
void Student::setclub()
{
cin>>number>>name>>score;
}
void Student::display()
{
cout<<number<<endl<<name<<endl<<score<<endl;
}
int main()
{
Student s1,s2,s3;
s1.setclub();
s2.setclub();
s3.setclub();
s1.display();
s2.display();
s3.display();
system("pause");
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询