Visual Studio Debug Assertion failed?!

Visual Studio throws and error with this code but web based compilers run it just fine? Specific error in VS is Debug Assertion failed! Expression: string iterator + offset out of range. Please help, any thoughts?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 #include <iostream>
#include <string>

using namespace std;

int main()
{
	string alpha = "abcdefghijklmnopqrstuvwxyz{";
	string::const_iterator x = alpha.begin(), x2;

	for (int p = 1; p <= 14; ++p) {
		int w, count = 0; // set to 0 each iteration

		// output spaces
		for (int k = 13; k >= p; --k)
			cout << ' ';

		x2 = x; // set starting point

		// output first half of characters
		for (int c = 1; c <= p; ++c) {
			cout << *x2;
			x2++; // move forwards one letter
			count++; // keep count of iterations
		}

		// output back half of characters
		for (w = 1, x2 -= 2; w < count; ++w) {
			cout << *x2;
			--x2; // move backwards one letter
		}

		x++; // next letter
		cout << '\n';
	}

	return 0;
}
Last edited on
Topic archived. No new replies allowed.