我正在从事一个项目,该项目涉及创建与大学名册有关的许多课程(名册,学生,枚举类型学位等)。我有一个我需要解析的学生数据列表,包含字符串和整数。我正在使用stoi转换我的整数,但我不断收到错误“以类型为std :: invalid_argument的未捕获异常终止:stoi:无转换”。我真的很想念这里吗?
我尝试重新排列,并从其他几种方法切换到stringstream,但是我仍然遇到相同的错误。
const string studentData[] =
{"A1, John, Smith, John1989@gm ail.com, 20, 30, 35, 30,
SECURITY",
"A2, Suzan, Erickson, Erikson_1990@gmailcom, 19, 50, 30, 40,
NETWORK",
"A3, Jack, Napoli, The_lawyer99yahoo.com, 19, 20, 40, 33,
SECURITY"
"A4, Erin, Black, Erin.black@comcast.net, 22, 50, 58, 40,
SECURITY",
"A5, Martin, Mart, martin.mart@comcast.net, 22, 30, 32, 35,
SOFTWARE"};
//add each student to the classRoster
for (int i = 0; i < 5; i++) {
//create stringstream to parse data
stringstream inSS(studentData[i]);
//create an empty vector
vector<string> studentInfo;
//check that stringstream is good and append values from
studentData to the studentInfo
while (inSS.good()) {
string segment;
getline(inSS, segment, ',');
studentInfo.push_back(segment);
}
Degree degree;
string studentDegreeProgram = studentInfo.at(8);
if (studentDegreeProgram == "SECURITY") {
degree = SECURITY;
}
else if (studentDegreeProgram == "NETWORKING") {
degree = NETWORKING;
}
else {
degree = SOFTWARE;
}
classRoster->Add(studentInfo.at(0), studentInfo.at(1),
studentInfo.at(2), studentInfo.at(3), stoi(studentInfo.at(4)),
stoi(studentInfo.at(5)), stoi(studentInfo.at(6)),
stoi(studentInfo.at(7)), degree);
}
答案 0 :(得分:0)
问题是,当您将没有整数值的字符串转换为int时,您尝试将其转换。使用stoi()时,“ SECURITY”是否具有等效的int值?否。使用stoi()时,“ 5”是否具有等效的int值?是的。