c++stringvisual-c++charindexoutofrangeexception

string subscript out of range in C++ for Cowculations


Please help with debugging.

It gives me an error 'string subscript out of range error' after the fifth input.

enter image description here

I was unable to figure out what to change.


Here is the code:

#include <iostream>
#include <string>
#define N 100

int str2int(std::string input)
{
    int num = 0;

    for (int i = 0; i < input.length(); i++)
    {
        if (input[i] == 'V')
            num *= 4 + 0;

        else if (input[i] == 'U')
            num *= 4 + 1;

        else if (input[i] == 'C')
            num *= 4 + 2;

        else if (input[i] == 'D')
            num *= 4 + 3;
    }
    return num;
}

int main(void)
{
    int tablet = 0, num1 = 0, num2 = 0, ans = 0;
    std::string cowNum1 = "", cowNum2 = "", result = "";
    std::string operation = "";

    std::cin >> tablet;
    std::cout << "COWCULATIONS OUTPUT" << std::endl;

    while (tablet--)
    {
        std::cin >> cowNum1 >> cowNum2;
        num1 = str2int(cowNum1);
        num2= str2int(cowNum2);
            
        for (int oprt = 0; oprt < 3; oprt++)
        {
            std::cin >> operation[oprt];

            switch (operation[oprt])
            {
            case 'A':
            {
                num2 += num1;
                break;
            }
            case 'R':
            {
                num2 >>= 2;
                break;
            }
            case 'L':
            {
                num2 <<= 2;
                break;
            }
            case 'N':
            default:
                break;
            }
        }
        std::cin >> result;
        ans = str2int(result);

        if (num2 == ans)
            std::cout << "YES" << std::endl;
        else
            std::cout << "NO" << std::endl;

        std::cout << "END OF OUTPUT" << std::endl;
    }
    return 0;
}

Question is from 377 - Cowculations

Sample Input

5
VVVVU
VVVVU
A
A
A
VVVVVVUV
VVCCV
VVDCC
L
R
A
VVVVUCVC
VVCCV
VVDCC
R
L
A
VVVVUCVV
VVUUU
VVVVU
A
N
N
VVVVVUCU
DDDDD
VVVVU
A
L
L
UVVVVVVV

Solution

  • You may not use the subscript operator for an empty string to change its value

     std::cin >> operation[oprt];
    

    At least you have to declare the object operation with the magic number 3 used in your for loop. For example

    std::string operation( 3, '\0' );
    

    Or

    std::string operation;
    operation.resize( 3 );
    

    If you need to enter only one characters in a loop then an object of the type std::string is not required. You could just write

        for (int oprt = 0; oprt < 3; oprt++)
        {
            char c;
            std::cin >> c;
    
            switch ( c )
            {
            case 'A':
            {
                num2 += num1;
                break;
            }
            case 'R':
            {
                num2 >>= 2;
                break;
            }
            case 'L':
            {
                num2 <<= 2;
                break;
            }
            case 'N':
            default:
                break;
            }
        }