what is wrong with the following code?

1
2
3
4
for (int i = 0; i <= phrase.size(); ++i)
{
cout << "Character at position " << i << " is: " << phrase[i] << endl;
}


If phrase is a string of size 3, the following letters exist:

phrase[0]
phrase[1]
phrase[2]

The following does NOT exist and is off the end of the string

phrase[3]

Now that you know this, you tell us what's wrong with the code.
oh i figured it out, but does he know that you cant do that becuase of what the result is?
i get it now, basically the i can't equal phrase size. so the it has to modified to i<phrase.size()
yep. because phrase size is one more than the last valid index of your array
Topic archived. No new replies allowed.