Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?

C#
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
Posted
Updated 7-Dec-12 4:31am
v2
Comments
André Kraak 7-Dec-12 9:38am    
Please share any relevant code with us, seeing the code might us help understand the problem you are facing.

If you wish to change your question use the Improve Question button.
saad_lah 7-Dec-12 10:33am    
srry for that, but actual code is too much inter-related with other classes and func. and it is not possible for me to post the complete project...this is another code with same error
lewax00 7-Dec-12 10:06am    
Stop trying to access indexes >= length of the string. Without seeing the code that's all I can figure out.

The problem is that if the data returned by your getline function is less than the length of stemp, you will get an error. For example, if the line it reads is empty, you will immediately get a problem.
C++
counter=1;
getline(myfile,sline);
iStudent_num++;
while(stemp[counter]==sline[counter])    //give error here while debugging

Try it: put a breakpoint on the getline, and see what it returns each time. Then add a check to ensure there are at least as many characters as you are going to compare against before you enter the loop!
 
Share this answer
 
Comments
saad_lah 7-Dec-12 10:42am    
got it..
in student.txt first line is '\n' so, sline gets "" which exceeds from the actual string length

Well, thanks for the help
OriginalGriff 7-Dec-12 10:54am    
You're welcome!
If your input is "l114441" I can see a problem right away - you keep going until counter is 7, but the length of the string is 7, and because it is 0 based the highest index is 6. On your final iteration, you try to access index 7, which would be the 8th character, but there isn't an 8th character.

For this code, you'd want to stop when counter is 6, not 7. I imagine your other code has a similar problem.
 
Share this answer
 
Comments
saad_lah 7-Dec-12 10:52am    
No, counter starts from 1 to 7, i am neglecting first character 'l' (common in all)
so sline[7] is correct, in case of sline[6] it will stop at l114443 and will not check the others because l11444 == l11444 satisfies
lewax00 7-Dec-12 10:59am    
Never mind, you're right, because it increments it before checking it, and never uses 7 as an index.
Why not use the std::string::compare[^] for this?
C++
if( !sline.empty() && sline.lenght() >= sSize && sline.compare( 0, sSize, stemp ) )
{
    student=fetch_data(sline);
    break;
}
 
Share this answer
 
Comments
saad_lah 7-Dec-12 10:59am    
No, it doesnot enters in 'if' condition and gives error "Not found"
although i have corrected it by my way

while(!myfile.eof())
{
counter=1;
getline(myfile,sline);
iStudent_num++;
size=sline.length();
if(size>0)
{
while(stemp[counter]==sline[counter])
{
counter++;
if(counter==7)
{
isFound=true;
break;
}
}
}
if(isFound==true)
{
student=fetch_data(sline);
break;
}
}

just by adding line if(size>0)
saad_lah 7-Dec-12 11:02am    
well thanks, i learned some new functions with strings just like sline.empty() or sline.compare( 0, sSize, stemp ) by your answer :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900