terminate called after throwing an instance of...

Hi guys,

I'm writing a program in C++ but now I get this error of which I've never heard
but I think I screwed something up.

the error is :

terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::at

it happens in this piece of code :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
for(i=1;i!=iCount;i++)
    {
        line = lines[i];
        sLine=line.at(0);
        if (sLine.compare("i") == 0)
        {
            //This is the ID value
            cout << "Found ID at line " << i << endl;
            line.replace(line.begin(), line.begin() + 3, "");
            aId[iCount2]=line; //We've set aId to line
        }

        if (line.compare("name=*") == 0)
        {
            //This is the Name value
            line.replace(line.begin(), line.begin() + 5, "");
            aName[iCount2]=line; //We've set aName to line
        }

        if (line.compare("loc=*") == 0)
        {
            //This is the Loc value
            line.replace(line.begin(), line.begin() + 4, "");
            aLoc[iCount2]=line; //We've set aLoc to line
        }

        if (line.compare("exe=*") == 0)
        {
            //This is the Exe value
            line.replace(line.begin(), line.begin() + 4, "");
            aExe[iCount2]=line; //We've set aExe to line
        }

        if (line.compare("parameter=*") == 0)
        {
            //This is the Parameter value
            line.replace(line.begin(), line.begin() + 10, "");
            aParameter[iCount2]=line; //We've set aParameter to line
            iCount2++;
        }
    }


The last 3 line.compare's don't work I know I didn't knew how it worked (string::compare)

But what is causing this error because it does work the way I want. :\

I hope you guys can help me.

-Just before posting I actualy think I know what's wrong-
(still posting because there's a chance I'm wrong)

Greetz,
AngelsDustz

edit :

I now know it's caused by sLine=line.at(0);

edit 2 :

I've found it, in the file it reads there are some empty lines, and if it tries to read character 0 from a line that contains nothing it crashes

edit 3 :


Here's the fix :
 
        if (line.length() > 0) {sLine=line.at(0);}
Last edited on
Topic archived. No new replies allowed.