【问题标题】:stoi throws out of range - c++ -stoi 抛出超出范围 - c++ -
【发布时间】:2022-06-14 20:12:07
【问题描述】:

我正在做这个程序,我从文件中读取信息并将它们附加到链接列表 但是每当我尝试使用 stoi 函数将 string 转换为 int 时,程序就会中断并且 stoi 会抛出“参数超出范围” 这是我正在开发的功能

void GetInputFromFile(ifstream &file)
{
    string line;
    file.open("courses.txt");
    while (getline(file, line))
    {
        
        int i = 0;
        cNodeCourse* pnnCourse = new cNodeCourse;
        cNodeStudent* pnnStudent = new cNodeStudent;
        string StudentID, StudentName, StudentAge, StudentGPA, CourseName, CourseCode;
        if (line[i] == '#')
        {
            i++;
            while (line[i] != ':')
            {
                CourseName += line[i];
                i++;
            }
            i++;
            while (i < line.size())
            {
                CourseCode += line[i];
                i++;
            }
            cout << "Got the course info" << endl;
        }
        if (line[i] == '*')
        {
            i++;
            while (line[i] != ',')
            {
                StudentID += line[i];
                i++;
            }
            i++;
            while (line[i] != ',')
            {
                StudentName += line[i];
                i++;
            }
            i++;
            while (line[i] != ',')
            {
                StudentAge += line[i];
                i++;
            }
            i++;
            while (i < line.size())
            {
                StudentGPA += line[i];
                i++;
            }
        }
        
        pnnCourse->pnext = NULL;
        pnnStudent->pnext = NULL;
        pnnStudent->ID = stoi(StudentID);
        pnnStudent->name = StudentName;
        pnnStudent->age = stoi(StudentAge);
        pnnStudent->gpa = stof(StudentGPA);
        pnnCourse->csname = CourseName;
        pnnCourse->cscode = CourseCode;
        pnnCourse->l.attach(pnnStudent);
        attach(pnnCourse);
    }
    file.close();
}

这是我的文本案例

#English:Eng101

*2131,ahmed,23,3.1

注意:我尝试使用 stoul、stol 和 stoll 我什至尝试了索引参数

【问题讨论】:

  • 您是否尝试使用调试器准确查看传递给 stoi 的内容,以及为什么?

标签: c++


猜你喜欢
  • 2016-05-15
  • 1970-01-01
  • 2012-05-04
  • 1970-01-01
  • 1970-01-01
  • 2014-05-23
  • 2013-11-18
  • 2021-06-26
  • 2013-09-28
相关资源
最近更新 更多