Question

Can you guys tell me why I get this error.


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.

Here is the code I found that has been causing the issue. Student for my test case is 30 characters. Also size is 10.

    for (count = 0; count < size; count++)
    {
              answers[count] = student[11 + (2*count)];
    }

I created size by the following method

    string examSize;
    fstream questionNumber;
    questionNumber.open("answers.txt", ios ::in);
    getline(questionNumber, examSize);
    int size = stoi(examSize);

I created answers with the following method.

     vector<char> answers(size,' ');

I created student with a string and it is occupied by a getline function.

      getline(studentAnswers, student);

Also could you guys tell me if there is a way for me to use more pointers in this code?

As of now I tried this method to check for an overflow in students I still get the same

error

    for (count = 0; count < size; count++)
    {
        cout << student[11 + count];
    }

No correct solution

OTHER TIPS

As the comments suggest, "string subscript out of range" means that you are trying to access an element of memory that is not part of the student object. You say you have limited the call to student[11] to student[20]. Are you sure this is in range? Try printing out the variables at each stage of the process, before and after statements that change the variable's value and every time around the loop and see if the data is what you think it should be.

I'm not sure what you are trying to do with this loop, but maybe it would be easier to extract the data you want out of the student array first, and then assign it to the answer array.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top