0

So I am writing a code to store each individual parts of a line to a specific class. For example I have to read a file containing IDS, last names, first names, birthdays, majors, and GPA. I successfully extracted the elements to my student class but when I read the file I always get this extra line? with the last element of the file. For example this is my file I am reading: [1.txt][1]

and when I try to compile I get this extra: [this is a picture of whats printing.][2] This is part of the code that deals with that:

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();

This is my student class:

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;
}

And date class:

#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
Prophed
  • 13
  • 3
  • It doesn't look like you're reading a GPA at all...or even storing more than one `Student` for that matter. – scohe001 Apr 12 '18 at 20:49
  • Yea I was just writing that part. else if (i == 4) { Student.setMajor(word); //major } else { float wordfloat; wordfloat = stof(word); Student.setGPA(wordfloat); // gpa } Then I have to store each element in arraylists and trees to then compare them. – Prophed Apr 12 '18 at 20:52
  • Without seeing how you're printing, how can we tell you what's wrong? – scohe001 Apr 12 '18 at 20:55
  • I did post what's printing as a picture, sorry I do not know how to post it so it always shows. – Prophed Apr 12 '18 at 20:57
  • Sorry, I mean to say without seeing the code to show the logic behind what you're printing, how can we tell you what's wrong? – scohe001 Apr 12 '18 at 20:59
  • I added my student and date class to it. – Prophed Apr 12 '18 at 21:09

0 Answers0