c++ - 从文件读取到不同的类对象

标签 c++ arrays class sorting fstream

我是 C++ 编码的初学者,我有一个关于将行从文件读入特定类对象的问题,其中一个是数组。

我的类对象:

const int SIZE_OF = 5;

class Student
{
public:
    Student();
    Student(const Student &);
    Student(string, int, int, int, int, int);
    friend std::istream& operator >> (std::istream& in, Student& S);
    void display();
private:
   string lastName;
   int grades[SIZE_OF];
};

定义函数的CPP文件:

Student::Student()
{
    int i;
    string lastName = "default";
    for (i = 0; i < 5; i++)
    {
        grades[i] = 0;
    }
}

Student::Student(const Student & S)
{
    int i;
    lastName = S.lastName;
    for (i = 0; i < 5; i++)
    {
        grades[i] = S.grades[i];
    }
}

Student::Student(string S, int a, int b, int c, int d, int e)
{
    lastName = S;
    grades[0] = a;
    grades[1] = b;
    grades[2] = c;
    grades[3] = d;
    grades[4] = e;
}

std::istream& operator >> (std::istream& in, Student& S)
{
    std::string line;
    std::getline(in, line);
    in >> S.lastName >> S.grades[0] >> S.grades[1] >> S.grades[2] >> S.grades[3] >> S.grades[4];
    getline(in, S.lastName);
    return in;
}

void Student::display()
{
    int i;
    int sum = 0;
    double average;
    cout << "Last Name: " << lastName << endl;
    cout << "Grades: " << endl;
    for (i = 0; i < 5; i++)
    {
        cout << grades[i] << endl;
    }
    for (i = 0; i < 5; i++)
    {
        sum = sum + grades[i];
    }
    average = sum / 5;
    cout << "Average: " << average;    
}

我需要做的是从文件中读取并将名称保存在 Student 类的 lastName 对象下。然后我需要将每个成绩保存在数组对象中。然后我需要为下一个名字创建一个新的 Student 类对象,并对 4 个名字重复该过程。我正在阅读的文件:

George
75,85,95,100,44
Peter
100,100,100,100,100
Frank
44,55,66,77,88
Alfred
99,88,77,66,55

最佳答案

您的以下功能似乎是错误的:

std::istream& operator >> (std::istream& in, Student& S)
{
    std::string line;
    std::getline(in, line);
    in >> S.lastName >> S.grades[0] >> S.grades[1] >> S.grades[2] >> S.grades[3] >> S.grades[4];
    getline(in, S.lastName);
    return in;
}

你读了两次 lastName,你没有忽略 ',' 字符......试试这样的事情:

std::istream& operator >> (std::istream& in, Student& S)
{
    char dummy;
    in >> S.lastName >> S.grades[0] 
       >> dummy >> S.grades[1] 
       >> dummy >> S.grades[2] 
       >> dummy >> S.grades[3] 
       >> dummy >> S.grades[4];

    return in;
}

更新: 如果考虑 lastName 内的空格,则必须更改第一行以使用 std::getline,如下所示:

std::istream& operator >> (std::istream& in, Student& S)
{
    char dummy;
    std::getline(in, S.lastName) 
    in >> S.grades[0] 
       >> dummy >> S.grades[1] 
       >> dummy >> S.grades[2] 
       >> dummy >> S.grades[3] 
       >> dummy >> S.grades[4];
    in.ignore(); // remove last '\n'
    return in;
}

然后:

int main()
{
    Student s;
    std::vector<Student> all;

    while (cin>>s)
      all.push_back(s);
}

...和...足够的更新!

关于c++ - 从文件读取到不同的类对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38513628/

相关文章:

python - 在 cygwin 中为 boost_python 链接 c++ 类

python - 列出 1 Dimensional Python Battleship 中的问题

java - 按字母顺序对字符串数组进行排序。如何按每行中间的数字对同一数组进行排序

python - 改变实例变量

c++ - 在析构函数中修改类成员对象是否会导致未定义的行为?

c++ - 如何解释这个奇怪的输出?关于指针和临时变量

arrays - 如何在 Swift 中实例化一个类(存储在数组中)?

java - 自定义矩形类不起作用

C++ - 访问全局变量对象

java - 将 Java 数组和基元 (double[][]) 重构为集合和泛型 (List<List<Double>>)