C++ 如何从文件中获取数据并添加到构造函数
我正试图从这个文件中获取数据,并将其添加到我的构造函数中。我理解我做错了什么,但我需要纠正。具体来说,在我的get grade函数中,如何将类对象设置为文件中的数据: 文件数据为:C++ 如何从文件中获取数据并添加到构造函数,c++,C++,我正试图从这个文件中获取数据,并将其添加到我的构造函数中。我理解我做错了什么,但我需要纠正。具体来说,在我的get grade函数中,如何将类对象设置为文件中的数据: 文件数据为: Mary Smith 10 5 8 10 6 2 10 Ken Lewis 5 3 2 10 8 9 10 我的代码: #include <iostream> #include <string> #include <fstream> #include &l
Mary Smith 10 5 8 10 6 2 10
Ken Lewis 5 3 2 10 8 9 10
我的代码:
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
void get_grade();
//void compute_grade();
class Student
{
public :
string firstname;
string lastname;
int lab1;
int lab2;
int lab3;
int lab4;
int lab5;
int lab6;
int lab7;
Student(string a, string b, int c, int d, int e, int f, int g, int h){
firstname = a;
lastname = b;
lab1 = c;
lab2 = d;
lab3 = e;
lab4 = f;
lab5 = g;
lab7 = h;
}
};
int main()
{
get_grade();
}
void get_grade()
{
//Student stud2();
ifstream myfile;
myfile.open("infile.txt");
myfile >> stud1.firstname;
cout << stud1.firstname;
}
#包括
#包括
#包括
#包括
使用名称空间std;
void get_grade();
//void compute_grade();
班级学生
{
公众:
字符串名;
字符串lastname;
int-lab1;
int-lab2;
int-lab3;
int-lab4;
int-lab5;
int-lab6;
int-lab7;
学生(字符串a、字符串b、整数c、整数d、整数e、整数f、整数g、整数h){
firstname=a;
lastname=b;
lab1=c;
lab2=d;
lab3=e;
lab4=f;
lab5=g;
lab7=h;
}
};
int main()
{
获得_等级();
}
void get_grade()
{
//学生研究2();
ifstreammyfile;
myfile.open(“infle.txt”);
myfile>>stud1.firstname;
在我看来,你似乎还没有完全掌握如何创建和使用类的实例。请花一些时间从a了解这些基本知识。当你还没有学会如何使用这些基本概念时,很难从你发布的代码中为你提供指导。有这么多学生在这么多的课程中提出了实验室和分数问题。你找不到其中任何一个吗?请试试看。