Hey guys for some reason I'm getting an extra number when I read data from a file and then try and use substr() on the string I just read in. Any help would be greatly appreciated!
Here's the data from the file I'm reading from:
i 35
i 56
i 69
Here's the output from the program with out the if statement:
i 34 35
i 56 57
i 69 70
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::substr: __pos (which is 2) > this->size() (which is 0)
Aborted (core dumped)
Output with the if statement:
i 34 35
i 56 57
i 69 70
70
while(!openFile.eof()) This is the problem. You should not loop on eof() unless you know how it is work. eof bit will not set until read operation will attempt to extract past-the-end symbol. So you will try to read one more line than there is in a file. This unexisting string is probably 0 length and attempt to call substring on it fails.
Quick fix:
1 2 3 4
while(getline(openFile, readLine))
{
c = (char)readLine[0];
//...