Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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
Posted 7-Dec-12 4:31am
saad_lah1.3K
Edited 7-Dec-12 5:31am
v2
Comments
André Kraak at 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 at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
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!
  Permalink  
Comments
saad_lah at 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 at 7-Dec-12 10:54am
   
You're welcome!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
saad_lah at 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 at 7-Dec-12 10:59am
   
Never mind, you're right, because it increments it before checking it, and never uses 7 as an index.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Why not use the std::string::compare[^] for this?
if( !sline.empty() && sline.lenght() >= sSize && sline.compare( 0, sSize, stemp ) )
{
    student=fetch_data(sline);
    break;
}
  Permalink  
Comments
saad_lah at 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 at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 6,474
1 OriginalGriff 6,258
2 Peter Leow 2,534
3 Abhinav S 2,274
4 Maciej Los 2,268


Advertise | Privacy | Mobile
Web01 | 2.8.150414.1 | Last Updated 7 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100