Expression: string subscript out of range

I'm having an issue with a function causing an error when running.

The function is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
wstring Tools::FormatPlainText(wstring Text) {
	wstring result = L"";
	wstring TextLocal;
	//Capitalise
	TextLocal = boost::to_upper_copy(Text);
	//Remove characters missing from charset
	wstring arrText = TextLocal;
	int i = sizeof(arrText) - 1;
	while (i > 0)
	{
		if (strchr(CHARSET, arrText[i]))
		{
			result += arrText[i];
		}
		i = i - 1;
	}
	return result;
};


The debugging comes back with this error:
Expression: string subscript out of range
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifdef _DEBUG
_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL _Debug_message(const wchar_t *message, const wchar_t *file, unsigned int line)
	{	// report error and die
        if(::_CrtDbgReportW(_CRT_ASSERT, file, line, NULL, L"%ls", message)==1)
        {
            ::_CrtDbgBreak();
        }
	}
_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL _Debug_message(const unsigned short *message, const unsigned short *file, unsigned int line)
	{	// report error and die
        _Debug_message((wchar_t *) message, (wchar_t *) file, line);
	}

#endif 


I know there is some wasteful code in here. I'm just playing with it trying to get it to work. All help is appreciated.
Last edited on
Be careful with your string types.
I agree. I went back and forth on what the correct type was to use. I've tried wstring and string but both create this error. I settled on wstring since I'm compiling on Windows.

Could you provide more insight that might help me with the issue?
Last edited on
> int i = sizeof(arrText) - 1;
http://en.cppreference.com/w/cpp/language/sizeof
sizeof does not give you the lenght of the string, for that use the member function `.lenght()'
Wow was I using that incorrectly then!!! Thank you!
Topic archived. No new replies allowed.