Debug Assertion Failed!

Not sure that this is the right place to post this since it may be IDE specific. I apologize if it belongs somewhere else. I originally wrote this in xcode and it worked fine - in Visual Studios 2012 however I get a runtime error.

I have a program that will compile and run correctly until a certain entry is input. The error that I am getting is the following:
---------------------------
Debug Assertion Failed!
Program: C:\Widnows\system32\MSVCP100D.dll
File: c:\program files (x86)\microsoft visual studio
11.0\vc\include\xstring
line:1662

Expression: string subscript out of range

For more information on how your program can cause an assertion
failure, see the visual c++ doccumentation on asserts.
------------------------------------

This project is a conversion program that takes a roman numeral string and converts is to arabic numbers. I have it set up to loop to keep asking the user for input until they give a quit command. I can enter in "I", "II", "III", "IV" and get the expected results. The minute I put in "V" or any other Roman Numeral I get the error

I have tried searching and can't figure out why I am getting that runtime error. If you think you can help - I would be happy to private message you my code. I am not going to post my code here because I know there are fellow students of mine trolling these forms looking for code to copy.

Thanks in advance for your help.
Looking closely in line 1662 should explain the error

-> http://www.cplusplus.com/forum/beginner/76422/
> I have tried searching and can't figure out why I am getting that runtime error.

The error message is quite explicit; it tells you why: Expression: string subscript out of range

1
2
3
4
5
std::string str = "abcd" ; // valid subscripts are 0, 1, 2, 3 
str[3] = '!' ; // fine
str[4] = '!' ; // *** error: string subscript out of range
str.substr( 8, 4 ) ; // *** error: string subscript out of range
// etc... 


The debugger will break at the assertion failure; examine the call stack to find out which line in your code triggered the error.


> Looking closely in line 1662 should explain the error

No, don't look at line 1662 in xstring; you didn't write the code for that. Look at the offending line in the code that you wrote.
Last edited on
@nevermore28 - Thanks for your response. I have evaluated my statements/loops for that same issue several times and the issue is still eluding me. Here is a little snippet of a Roman numeral that may be evaluated in the string - thie roman numeral, when input, will cause that run time error.
1
2
3
4
5
6
7
8
9
10
11
12
 for (size_t position = 0; position < m_length; position++) // m_length is m_inputString.size()
	{
        switch(m_inputString[position])
        {
            case 'M':
                if(m_inputString[position - 1] == 'C')
                {
                    m_total += 0;
                }
                else
                    m_total += 1000;
                break;


From what I understand from the link you posted - my statement is going out of bounds - if that is the case, can you point me in the right direction as to how to force it to stay within bounds?
1
2
3
4
5
6
7
8
for (size_t position = 0; position < m_length; position++) // m_length is m_inputString.size()
	{
        switch(m_inputString[position])
        {
            case 'M':
                if(m_inputString[position - 1] == 'C') // **** ????
                
                // *** 


What will happen when the first character (at position zero) in m_inputString is an 'M'?
@JLBorges - Thanks for your responses. If 'M' is in position zero it should add 1000 to the total. That is what the else statement (see below) is doing - unless I am missing something...
1
2
else
                    m_total += 1000;


@JLBorges, i'm sorry i'm wrong, it didn't come to my head

@programmingnub, what if position is 0, and the condition is true, you will try to access m_inputString[ -1 ] which is invalid ??
@nevermore28 - Correct me if I am wrong - but wouldn't it be possible to for the location to -1? Since it will search the string for 'M' then check if the position is before it is 'C' - that means in order for the condition to be true it, 'M' would have to be at least one position after 'C'.

I may not be wrapping my head around what you are saying. Sorry, I'm new to this :)
wouldn't it be possible to for the location to -1

yes but it is a garbage data:

supposed this condition:

string var = "MCVI"; // roman numeral

what does this look on memory:

x | M C V I ( the string )
---------------------------
-1 | 0 1 2 3 ( the index )

where x is some random data you are trying to access which don't belong to the memory reserved to contain var

Really i don't know if this is the EXACT explanation so if someone could
correct me i will really appreciate it but i think this is sufficient


-----------------
Since it will search the string for 'M' then check if the position is before it is 'C'

my point is in your if condition and the accesing of element

say position is 0 and the case is true:
1
2
3
4
position = 0;

case 'M' : // say m_inputString[0] == 'M';
    if ( m_inputString[position - 1] == 'C')  // you are accessing out of bounds ( -1 )  


---------------------
that means in order for the condition to be true it, 'M' would have to be at least one position after 'C'

yes
@nevermore28 - Thanks! I see what you are talking about. If I understand you correctly then the problem is not that I am actually finding a variable in the -1 position - it's that I am checking that location to begin with.

Is there even a way to correct that with the way my code is set up? I've been racking my brain and am unable to think of one with the knowledge I have. How would I check the position before M for without going into -1 if M is in position zero??
Actually - I think I figured it out. I'll let you know if what I am thinking doesn't work!
Hmmm....So originally I thought I could just go through all my statements and change them to be something like this:

1
2
3
4
5
6
7
8
case 'M':
                if(m_inputString[position >= 1] == 'C')
                {
                    m_total += 0;
                }
				else
                    m_total += 1000;
                break;


That works - but is their a more efficient way of resolving the issue?
Topic archived. No new replies allowed.