Looking to add comma delimited values in a text fi

2019-08-29 01:49发布

问题:

I have a text file here with two values, a name and a score. I have a student struct that has 4 members which are seen below.

I am looking to add the values in the text file to the corresponding members in the struct separating by comma.

First five rows of the students.txt file;

Nubia,Dufrene,70
Louisa,Trippe,49
Aline,Deniz,34
Shery,Munk,63
Angila,Ping,89

My current code;

struct studentType {
    string studentFName;
    string studentLName;
    int testScore;
    char grade;
};


int main()
{
    vector<studentType> studentVector;
    studentType student;


    ifstream inFile("students.txt");

    while (getline(inFile, student.studentFName, ',' )) {
        cout << student.studentFName << endl;
    }



        printStudents(studentVector);
}


void printStudents(vector<studentType>& passedVect) {

    for (studentType element : passedVect) {
        cout << element.studentFName << " " << element.studentLName << "\tGrade: " << element.testScore << " (" << element.grade << ")" << endl;
    }

}

The FIX I've replaced the while loop with a for loop. I also had to change the struct from int to string for it to work with getline. The simple convertString function uses std::stoi to convert it back to an int as originally planned.


int main()
{
    vector<studentType> studentVector;


    studentType student;

    ifstream inFile("students.txt");

    for (studentType i;
        getline(inFile, i.studentFName, ',')
        && getline(inFile, i.studentLName, ',')
        && getline(inFile, i.testScore)
        ; ) 
    {

        int testScore = convertString(i.testScore);
        i.grade = assignGrade(testScore);

        studentVector.push_back(i);
    }
    printStudents(studentVector);
}


int convertString(string number) {

    return stoi(number);
}

Output

Struct Exercises!
Nubia Dufrene           Grade: 70 (B)
Louisa Trippe           Grade: 49 (D)
Aline Deniz             Grade: 34 (F)
Shery Munk              Grade: 63 (C)
Angila Ping             Grade: 89 (A)
Laila Hollmann          Grade: 10 (F)
Sherrill Piller         Grade: 47 (D)
Minna Dimitri           Grade: 26 (F)
Song Kornreich          Grade: 97 (A)
Frank Dammann           Grade: 36 (F)
Isaac Abee              Grade: 24 (F)
Tiffaney Lukach         Grade: 75 (B)
Carmelina Sink          Grade: 85 (A)
Matthew Benes           Grade: 34 (F)
Fleter Aichele          Grade: 78 (B)
Sergio Ewan             Grade: 56 (C)
Izetta Armes            Grade: 42 (D)
Olen Tee                Grade: 89 (A)
Leona Mozee             Grade: 54 (D)
Britta Pegrast          Grade: 34 (F)

Thanks again!

回答1:

struct studentType {
    string studentFName;
    string studentLName;
    string testScore;
    char grade;
};

void printStudents(vector<studentType>& passedVect );

int main()
{
    vector<studentType> studentVector;
    studentType student;


    ifstream inFile("students.txt");

    while (getline(inFile, student.studentFName, ',' ) 
        && getline(inFile, student.studentLName, ',') 
        && getline(inFile, student.testScore) ) {
        cout << student.studentFName << " " 
             << student.studentLName << " " 
             << student.testScore << endl;
    }
}

I just concated them and changed the testScore to a string



回答2:

You can use std::stringstream to first save each row and than use the same strategy

getline(inFile, student.studentFName, ',' )

except now on the stringstream to fill the 3 variables. Now, that the Student is filled, you can push_back it into your vector and afterwards call your printStudents function.

I removed the using namespace std; as it is bad practise (you can read here why). You also didn't provide a grade in your text file, so I removed that part when printing the students.

#include <string>
#include <fstream>
#include <iostream>
#include <vector>
#include <sstream> //std::stringstream


struct studentType {
    std::string studentFName;
    std::string studentLName;
    int testScore;
    char grade;
};

void printStudents(const std::vector<studentType>& passedVect) { //also: const&!

    for (studentType element : passedVect) {
        std::cout << element.studentFName << " " << element.studentLName << "\tScore: " << element.testScore << '\n';
    }

}

int main() {
    std::vector<studentType> studentVector;
    studentType student;


    std::ifstream inFile("students.txt");
    if (!inFile.good()) {
        std::cerr << "couldn't find student.txt file\n";
        return -1;
    }

    std::string line;

    while (std::getline(inFile, line)) {
        std::stringstream stream(line);

        std::getline(stream, student.studentFName, ','); //get first name
        std::getline(stream, student.studentLName, ','); //get last name

        std::string score_str; //since testScore is an int, first save it as a string
        std::getline(stream, score_str, ',');
        student.testScore = std::stoi(score_str); //convert string to int

        studentVector.push_back(student); //push it into vector
    }

    printStudents(studentVector);
}

output:

Nubia Dufrene   Score: 70
Louisa Trippe   Score: 49
Aline Deniz     Score: 34
Shery Munk      Score: 63
Angila Ping     Score: 89