[英]Looking to add comma delimited values in a text file to members of a struct
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.
我有一个学生结构,有4个成员,如下所示。
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; 前五行的students.txt文件;
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. FIX我用for循环替换了while循环。 I also had to change the struct from int to string for it to work with getline.
我还必须将struct从int更改为string以使其与getline一起使用。 The simple convertString function uses std::stoi to convert it back to an int as originally planned.
简单的convertString函数使用std :: stoi将其转换回原计划的int。
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! 再次感谢!
You can use std::stringstream
to first save each row and than use the same strategy 您可以使用
std::stringstream
首先保存每一行,然后使用相同的策略
getline(inFile, student.studentFName, ',' )
except now on the stringstream to fill the 3 variables. 除了现在在stringstream上填充3个变量。 Now, that the Student is filled, you can
push_back
it into your vector
and afterwards call your printStudents
function. 现在,学生被填满,你可以
push_back
到您的vector
并随后打电话给你printStudents
功能。
I removed the using namespace std;
我删除了
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. 您也没有在文本文件中提供
grade
,因此我在打印学生时删除了该部分。
#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
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 我刚刚将它们汇总并将testScore更改为字符串
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.