问题描述
- 为什么输出结果与输入结果不一样c++
-
#include
#include
#include
#include
#includeusing namespace std;
class students
{
char ID[20];
char name[10];
char sex[3];
int age;
char grade[30];
public:
students();
char *gets_ID(); //学号、姓名、性别、年龄、班级
char gets_name();
char gets_sex();
int gets_age();
char gets_grade();};
char * students::gets_ID()
{
return ID;
}char students::gets_name()
{
return *name;
}
char students::gets_sex()
{
return *sex;
}
int students::gets_age ()
{
return age;
}
char students::gets_grade ()
{
return *grade;
}students::students()
{
cout <<"请输入学号:";
cin>>ID;
cout <<"请输入姓名:";
cin>>name;
cout <<"请输入性别:";
cin>>sex;
cout << "请输入年龄:";
cin>>age;
cout <<"请输入班级:";
cin >>grade;}
main()
{
students stu;
// students();
cout <<stu.gets_ID <<' '<<stu.gets_name <<' '<<stu.gets_sex <<' '<<stu.gets_age <<' '<<stu.gets_grade <<endl;
//cout <<stu.gets_age<<' '<<stu.gets_grade <<endl;
}
解决方案
return 时,如果是字符串,不要使用 *,而是直接使用指针。