axyd icon

C++ File to Vector of Objects

axyd | PRO | 10/16/16 05:33:53 AM UTC | 0 ⭐ | 664 👁️ | Never ⏰ | []
C++ |

1.83 KB

|

None

|

0 👍

/

0 👎

#####################
####  headers.h  ####
#####################
#ifndef SOMETHING_H
#define SOMETHING_H
#include <iostream>
 
struct studentinfo {
    int id;
    string surname;
    string name;
    string year;
    int taken;
    int grad;
    string major;
    string minor;
 
    studentinfo(int, string, string, string, int, int, string, string);
};
#endif
 
#####################
#### methods.cpp ####
#####################
 
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <sstream>
#include "headers.h"
using namespace std;
 
studentinfo::studentinfo(int i, string sn, string nm, string yr, int tkn, int g, string ma, string mi)
    :id(i),surname(move(sn)),name(move(nm)),year(move(yr)),taken(move(tkn)), grad(move(g)),major(move(ma))
    ,minor(move(mi)) {};
 
 
vector<studentinfo> CollegeStudent::vstuds() {
//remove header row junk -  Adam @ http://stackoverflow.com/a/164367/6793751
    vector<studentinfo> vTmp;
    fstream f(rosterfile);
    string junk;
    getline(f,junk,'\n'); //skip header row
 
    string line=" ";
    int rktr=0; //rows
    while(getline(f,line,'\n')) {
        studentinfo *si= new studentinfo(0,"","","",0,0,"","");
        stringstream ss(line);
        string in_line;
        int cktr=0; //cols
        vector<string> vstest;
        while(getline (ss, in_line, ',')) {
            vstest.push_back(in_line);
            if(cktr==0) (*si).id=       stoi(in_line);
            if(cktr==1) (*si).surname= in_line;
            if(cktr==2) (*si).name=     in_line;
            if(cktr==3) (*si).year=     in_line;
            if(cktr==4) (*si).taken=    stoi(in_line);
            if(cktr==5) (*si).grad=     stoi(in_line);
            if(cktr==6) (*si).major=    in_line;
            if(cktr==7) (*si).minor=    in_line;
            ++cktr;
        }
 
        vTmp.emplace_back(*si); //could also receive raw values w/o object or constructor (0,"blah, 0.1,'x',....)
 
        cout<<vTmp.at(rktr).year<<endl;
        delete si;
        rktr++;
    }
    return vTmp;
}

Comments