Hey, I desire to recognize how have the right to i rid that "string subscript the end of range" error?Or just how to initialize the wire to get rid of this error?

bool Find_Student(Administrator &student) cable 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==sline) //give error right here while debugging counter++; if(counter==7) isFound=true; break; //////Leave the remainder 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 offered in Roll_num is l114441
Please share any relevant code v us, see the code might us assist understand the difficulty you room facing.

You are watching: String subscript out of range c++

If you wish to adjust your question use the enhance Question button.
srry because that that, however actual code is too lot inter-related with other classes and func. And it is not feasible for me to post the finish project...this is one more code with same error
Stop do the efforts to access indexes >= length of the string. Without see the code that"s all ns can figure out.

The trouble is the if the data went back by her getline role is much less than the length of stemp, you will gain an error. Because that example, if the heat it reads is empty, girlfriend will immediately get a problem.

counter=1;getline(myfile,sline);iStudent_num++;while(stemp==sline) //give error right here while debuggingTry it: put a breakpoint on the getline, and also see what it returns every time. Then include a examine to ensure there room at least as many personalities as you are going to compare versus before you enter the loop!
got it..in student.txt first line is " " so, sline it s okay "" which exceeds from the yes, really string length Well, thanks for the help
If her input is "l114441" I have the right to see a problem right away - you keep going until counter is 7, yet the length of the cable is 7, and because it is 0 based the greatest index is 6. Top top your final iteration, you try to access index 7, which would certainly be the 8 hours character, yet there isn"t an 8th character.For this code, you"d desire to stop when counter is 6, not 7. I imagine your other code has actually a comparable problem.
No, respond to starts native 1 come 7, i am neglecting very first character "l" (common in all)so sline<7> is correct, in instance of sline<6> the will stop at l114443 and also will not examine the others due to the fact that l11444 == l11444 satisfies
Never mind, you"re right, because it increments it prior to checking it, and never provides 7 as an index.

See more: Never Go Against A Sicilian When Death Is On The Line, Classic Blunders

if( !sline.empty() && sline.lenght() >= sSize && sline.compare( 0, sSize, stemp ) ) student=fetch_data(sline); break;
No, it doesnot start in "if" condition and also gives error "Not found"although i have corrected it by mine waywhile(!myfile.eof())counter=1;getline(myfile,sline);iStudent_num++;size=sline.length();if(size>0)while(stemp==sline)counter++;if(counter==7)isFound=true;break;if(isFound==true)student=fetch_data(sline);break;just by adding line if(size>0)
well thanks, ns learned some brand-new functions v strings similar to sline.empty() or sline.compare( 0, sSize, stemp ) by your answer :)
I have actually read and agree come the terms of Service and Privacy policy Please subscribe me come the jamesmerse.com newsletters
when answering a question please: read the inquiry carefully.Understand the English isn"t everyone"s very first language so it is in lenient of badspelling and grammar.If a inquiry is poorly grammar then one of two people ask because that clarification, ignore it, oredit the question
and fix the problem. Insults space not welcome. Don"t tell who to check out the manual. Opportunities are lock have and also don"t gain it. Administer an price or move on come the next question. Let"s occupational to assist developers, not make them feeling stupid.

This content, in addition to any associated resource code and files, is licensed under The code Project open License (CPOL)