使用c++,定义一个学生类(姓名,成绩),然后再定义一个学生数组,把三个学生的信息存入纯文本文件中

使用c++,定义一个学生类(姓名,成绩),然后再定义一个学生数组,把三个学生的信息存入纯文本文件中照片中是我编写的,不报错,但是名字存不到文件中,各位大神给我改改
#include<fstream>
#include<iostream>
#include<string>
using namespace std;
class student{
public:
char name[11];
int chinese;
int math;
public:
void grade(){int i=0;
string name; cin>>name;
cin>>chinese>>math;
cout<<"姓名"<<name<<'\t'<<"语文"<<chinese<<'\t'<<"数学"<<math<<endl;}};
void main()
{student aa[3];
int i=0,j;
for(i;i<3;i++)
aa[i].grade();
ofstream ofile;
ofile.open("d:\\myfile.txt");

for(i=0;i<3;i++) { for(j=0;j<10;j++) ofile<<aa[i].name[j];
ofile<<aa[i].chinese<<'\t'<<aa[i].math<<endl;}
ofile.close();

}

第1个回答  2018-09-25
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class student
{
public:
    student(string name, int score):name(name),score(score){}
    friend ostream& operator<<(ostream&, const student&);
private:
    string name;
    int score;
};

ostream& operator<<(ostream& os, const student& s)
{
    os<<s.name<<' '<<s.score<<endl;
}

int main()
{
    student stu[]={student("Alice", 90), student("Bob", 91), student("Charlie", 92)};
    ofstream out("student.txt");
    out<<stu[0]<<stu[1]<<stu[2];
    out.close();
    return 0;
}

本回答被网友采纳
第2个回答  2018-09-25
淮上喜会梁州故人(韦应物)
相似回答