本文介绍了字符串下标超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 嘿, 我想知道如何摆脱字符串下标超出范围的错误? 或者如何初始化字符串以消除此错误? bool Find_Student (管理员和学生) { string sline,stemp; int counter = 1 ; bool isFound = false ; int iStudent_num = 0 ; stemp = student.return_roll(); // 从类中获取卷号,工作正常 int sSize = stemp.length(); ifstream myfile( student.txt); while (!myfile.eof()) { counter = 1 ; getline(myfile,sline); iStudent_num ++; while (stemp [counter] == sline [counter]) // 在调试时给出错误 { counter ++; if (counter == 7 ) { isFound = true ; break ; } } /// ///留下剩下的东西/ //// if (isFound == true ) { student = fetch_data(sline); break ; } } myfile.close(); return isFound; } /////////student.txt/////// l114443 $ Talha Jamil $ samsung $ 12 $ 17 $ 2004 $ UnderGraduate $! l114445 $ Kamran Javed $ jannat123 $ 12 $ 12 $ 2009 $ UnderGraduate $! l114093 $ Imran Khan $ pluto123 $ 11 $ 4 $ 2009 $ UnderGraduate $! l114444 $ Jamil Ahmed $ origin123 $ 11 $ 12 $ 2009 $ UnderGraduate $! l114606 $ Zain Malik $ samsung $ 9 $ 16 $ 2009 $毕业生$! l114441 $ Saad Kamran $ samsung $ 11 $ 4 $ 2004 $毕业生$! 假设输入在Roll_num中给出的是l114441 解决方案 Talha Jamil samsung 12 Hey, I want to know how can i rid of "string subscript out of range" error?Or how to initialize the string to get rid of this error?bool Find_Student(Administrator &student){ string sline,stemp; int counter=1; bool isFound=false; int iStudent_num=0; stemp=student.return_roll(); //gets roll number from class, works fine int sSize=stemp.length(); ifstream myfile("student.txt"); while(!myfile.eof()) { counter=1; getline(myfile,sline); iStudent_num++; while(stemp[counter]==sline[counter]) //give error here while debugging { counter++; if(counter==7) { isFound=true; break; } } //////Leave the rest of stuff///// if(isFound==true) { student=fetch_data(sline); break; } } myfile.close(); return isFound;}/////////student.txt///////l114443$Talha Jamil$samsung$12$17$2004$UnderGraduate$!l114445$Kamran Javed$jannat123$12$12$2009$UnderGraduate$!l114093$Imran Khan$pluto123$11$4$2009$UnderGraduate$!l114444$Jamil Ahmed$origin123$11$12$2009$UnderGraduate$!l114606$Zain Malik$samsung$9$16$2009$Graduate$!l114441$Saad Kamran$samsung$11$4$2004$Graduate$!Suppose Input given in Roll_num is l114441 解决方案 Talha Jamilsamsung12 这篇关于字符串下标超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-14 16:10