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:
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 (unsignedint 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.
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)
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.
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.