读取文件后,最后一个对象重复

时间:2018-04-12 20:47:06

标签: c++ loops ifstream

所以我正在编写一个代码来将一行的每个部分存储到特定的类中。例如,我必须阅读包含IDS,姓氏,名字,生日,专业和GPA的文件。我成功地将元素提取到了我的学生班,但是当我读到文件时,我总是得到这个额外的行?与文件的最后一个元素。例如,这是我正在阅读的文件:[1.txt] [1]

当我尝试编译时,我得到了额外的内容:[这是什么打印的图片。] [2] 这是处理该问题的代码的一部分:

student Student;
ifstream file("1.txt");
string word;
while (!file.eof()) {
    for (int i = 0; i < 6; i++) {
        getline(file, word,',');
        cout << word;
        if (i == 0) {
            //word.erase(word.length() - 1);
            Student.setID(word); // id
        }
        else if (i == 1) {
            //word.erase(word.length() - 1);
            Student.setFirstname(word); // first name
        }
        else if (i == 2) {
            //word.erase(word.length() - 1);
            Student.setLastname(word); // last name
        }
        else if (i == 3) {
            string word1;
            string word2;
            string word3;
            int num1;
            int num2;
            int num3;
            word1 = word.substr(0, 5);//year
            num1 = stoi(word1);
            Student.setYear(num1);
            word2 = word.substr(6, 3);// month
            num2 = stoi(word2);
            Student.setMonth(num2);
            word3 = word.substr(9, 3); // day
            num3 = stoi(word3);
            Student.setDay(num3);
        }
        else if (i == 4) {
            Student.setMajor(word); //major
        }
        else {
            float wordfloat;
            wordfloat = stof(word);
            Student.setGPA(wordfloat); // gpa
        }
    }
}
file.close();

这是我的学生班:

class student : public date {
private:
string ID;
string firstname;
string lastname;
string major;
float GPA;

public:
student() {
    ID = "";
    firstname = "";
    lastname = "";
    major = "";
    GPA = 0.0;
}



student(string Id, string Firstname, string Lastname, string Major, float GPA1, int Day, int Month, int Year) {
    ID = Id;
    firstname = Firstname;
    lastname = Lastname;
    major = Major;
    GPA = GPA1;
}

string getID() {
    return ID;
}

string getFirstname() {
    return firstname;
}

string getLastname() {
    return lastname;
}

string getMajor() {
    return major;
}

float getGPA() {
    return GPA;
}

void setID(string a) {
    ID = a;
}

void setFirstname(string b) {
    firstname = b;
}

void setLastname(string c) {
    lastname = c;
}

void setMajor(string d) {
    major = d;
}

void setGPA(float e) {
    GPA = e;
}

日期类:

#ifndef DATE_H
#define DATE_H
#include <iostream>
using namespace std;

class date {
private:
    int day;
    int month;
    int year;

public:
    date() {
        day = 0;
        month = 0;
        year = 0;
    }

    date(int Day, int Month, int Year) {
        day = Day;
        month = Month;
        year = Year;
    }

    int getDay() {
        return day;
    }

    int getMonth() {
        return month;
    }

    int getYear() {
        return year;
    }

    void setDay(int a) {
        day = a;
    }

    void setMonth(int b) {
        month = b;
    }

    void setYear(int c) {
        year = c;
    }



};
#endif

0 个答案:

没有答案