I have stumbled upon an exercise on Michael Dawson's book: Beginning C++ Game Programming. It asks to find what is wrong with the following code. I think it is the <= operator, but i have my doubts. Any ideas? Here is the code:
1 2
for (int i = 0; i <= phrase.size(); ++i)
cout << “Character at position “ << i << “ is: “ << phrase[i] << endl;
Yes, the size() function gives the number of elements in that array, since arrays count from 0 and not 1, there will be no n'th element for an array of size n.
Please input a phrase:
test
With phrase.size()
Character at position0 is: t
Character at position1 is: e
Character at position2 is: s
Character at position3 is: t
Character at position4 is: ?
With phrase.size() - 1
Character at position0 is: t
Character at position1 is: e
Character at position2 is: s
Character at position3 is: t