out_of_range and I don't know why it's throwing

Hey there, so I'm having a problem with substr. I'm getting an out_of_range error but according to my debugging and the compiler, the pos is less than the length of the string. Here's the 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
43
44
45
46
47
48
49
50
51

void get_words( map <string, int >&m )
{
//calls the ifstream to read in strings
//sends them through clean entry
//inputs everything from clean entry into a map


string s1, s2, line;
ifstream myfiles("prog4.d");


if (myfiles.is_open())
{
 while (!myfiles.eof())
 {


 getline(myfiles, line) ;
 line += '\n';
 size_t index = 0;
  for (unsigned int i = 0; i <= line.size(); i++) //creating iterator to move through and
  {                                                     //get words without spaces
   if(line[i] == ' ' || line[i] == '\n' )                                                
   { size_t len = i; //creates the string the second that it sees a space
        cout << "Space found at position: " << len << endl;
        cout << "Index: " << index << " " << "Length afterwards: " << len-index
        << endl;
cout << "String Length: " << line.length() << endl;

        s1 = line.substr (index, len-index);

        cout << "string: " << s1 << endl;
         if (s1.length() > 0)
         {
         clean_entry(s1, s2); //cleans the string and adds it to the map
         m[s2]++;

         s1.clear();
         s2.clear(); //clears the string so that it can be refilled
         index = len;} //puts pos to the front where the process can restart.
    }
   }
  }
  myfiles.close();
}
else
        { cout << "Could not open file" << endl;
        exit (-1);
        }
}


The data file I'm inputting into it looks like this:


                                     PREFACE

This is a book about the computer language called C. If you are
seeking a book to increase your typing speed, expand on your
knowledge of word processing, or learn the secrets of chip
fabrication and design, this is not the one for you. However , if
you want to become thoroughly familiar with the C programming
language, then you have made a wise choice. For this book is devoted
to just that--to help you become proficient in C.

        It is one thing to read about a language; it is quite another to
get involved in it.. The best and most time-effective way to absorb
a language such as C is to have 1 terminal or computer available to
you , preferably at you fingertips. You will be exposed  to well
over 100 C programs in this book.

        You are encouraged to experiment with the programs illustrated
in this text. Omit keywords, change a comma to a period, ,
deliberately forget to terminate a statement with the required
semicolon, etc. You can do whatever you want without having to worry
about causing any damage to the computer, because you simply can't.
Play "what if" games to your heart's content. Familiarity with the
language will bring with it greater understanding ;the more you
understand about "C", the more you will enjoy it. ??? 


Error occurs around "However , if". Any idea what this could be? I'm fairly certain pos is less than strlen. Soooo...I have no idea what this could be.
Last edited on
Did you try this just before the substr() call
cout << "index : " << index << " len-index : " << len-index << endl;
If you mean that code, yes.
So you know for certain what the values are at the point when it gives the error. That ought to give you a pretty big clue. Out of interest, what were the values? (line.length() as well)
Here's the output:

Space found at position: 62
Index: 60 Length afterwards: 2
String Length: 66
string:  ,
terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::substr
Aborted (core dumped)


Normally I'd agree, but I'm stumped. You can see that index is 60, string length is 66, so why it is throwing out_of_range is beyond me.
Is the problem in clean_entry()? It runs fine here with a couple of modifications.

line 36 - //clean_entry(s1, s2); // function not provided
line 37 - m[s1]++;

Added a main():
1
2
3
4
5
6
7
int main(){
    map<string, int>  m;
    get_words(m);
    map<string, int>::iterator it;
    for(it=m.begin(); it!=m.end();++it)
        cout << it->first << "\t" << it->second << endl;
}


output:
51
"C", 1
"what 1
, 3
1 1
100 1
;the 1
??? 1
C 3
C. 2
Familiarity 1
. . .
. . .
understand 1
you 2
The fact that the text "string: ," is printed indicates that execution got past the substr() at line 31.

I suspect norm b is right, there must be another substr() call, most likely in the clean_entry()
There is another substr call. I will check it out.
Thanks again everyone for your help. I really really appreciate it. I figured most of the problem out. Basically, in my function clean_entry, it doesn't know what to do if it can't find an alphanumeric so I created if statements to prevent the passing in of a blank string or a string filled only with punctuation. ne555, no I didn't know it, but man that sure looks an awful lot like the assignment. That was...very embarassing to say the least. I had figured that my substr in the clean_entry function was working fine. I'll be sure to edit it out for the future. Again. Thank you so much.
Topic archived. No new replies allowed.