for loop OR string::at issue

Hey!

So I am trying to make a for loop that uses 'i' (the counter/increment) as the parameter (argument?) for a string.at() function.

1
2
3
4
      for (int i = 0; i <= eqlength; i++){
        cout << i << endl;
        cout << equation.at(0+i);
    }


When this loop runs, the loop works, however, the console terminates itself with the following:


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


Why does it terminate itself instead of continuing with the program? Does the issue lay within the string.at() or the for loop?
looks like your 'eqlength' is a bigger value than the number of elements in your 'equation' string.

as an aside, why put 'at(0+i)'? rather than just 'at(i)'?

edit: i have a feeling that this:
for (int i = 0; i <= eqlength; i++)

should be:
for (int i = 0; i < eqlength; i++)
Last edited on
Thanks mutexe! Your edit fixed the issue.

And I only put the at(0+1) in a desperate attempt to solve the issue >_<

Thanks again!

SOLVED
no worries. just remember if you've defined an array with ten elements for example, you access the elements from 0 to 9. element 10 does not exist.
Topic archived. No new replies allowed.