首页 > 解决方案 > 如何从文件中获取数据并添加到构造函数

问题描述

我正在尝试从此文件中获取数据并将其添加到我的构造函数中。我做错了什么,我明白,但我需要更正。具体来说,在我的获取等级函数中,如何将类对象设置为文件中的数据:

文件数据为:

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;


}

标签: c++

解决方案


推荐阅读