希望在文本文件中将逗号分隔的值添加到结构的成员 [英] Looking to add comma delimited values in a text file to members of a struct

查看:118
本文介绍了希望在文本文件中将逗号分隔的值添加到结构的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有一个文本文件,其中包含两个值,一个名称和一个分数. 我有一个具有4个成员的学生结构,如下所示.

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.

students.txt文件的前五行;

First five rows of the students.txt file;

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

我当前的代码;

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

}

FIX 我已将for循环替换为while循环. 我还必须将结构从int更改为string才能与getline一起使用.简单的convertString函数使用std :: stoi将其转换回最初计划的int值.

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

输出

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)

再次感谢!

推荐答案

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

我只是将它们收成空,并将testScore更改为字符串

I just concated them and changed the testScore to a string

这篇关于希望在文本文件中将逗号分隔的值添加到结构的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆