Error Statement

I got an error statement here, i can't find it. can you help me?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  #include <iostream>
#include <string>

using namespace std;

int main()
{
	string phrase = "Game over";
	
	for (int i = 0; i <= phrase.size(); ++i)
	{
		cout << "Character at position " << i << " is: " << phrase[i] << endl;
	}
}
for (int i = 0; i <= phrase.size(); ++i) Here is your problem. You are accessing out of bounds at the last iteration. Use < instead.
I got an error statement here

How do you get that error? Is it a compiler error? A linker error? A runtime error?

What is the exact error message?

It is good to learn to provide the essential details when asking questions. Sometimes you can even spot the error yourself, when gathering the details.


In this case, how long is (a piece of) string? What are the valid indices for an N-element long array in C++?
Topic archived. No new replies allowed.