Roman Numeral conversion program throwing an exception. Newbie question

-3

The exception is "Exception thrown at 0x0F71514F (vcruntime140d.dll) in ConsoleApplication28.exe: 0xC0000005: Access violation writing location 0xCCCCCCCC. occurred"

It seems to have something to do with the three lines

            lowerOutput = twoColumn[i][1];
            lowerOutputSecond = toLower(lowerOutput);
            twoColumn[i][2] = lowerOutputSecond;

and for the life of me I can't figure out what the problem is. I can take out these lines and it works ok although it's incomplete. Any help would be appreciated.

    #include <iostream>
    #include <string>


    using namespace std;

    string toLongRoman(int a);
    string toLower(string s);

    int main() {

        int input = 1, error = 1, oneColumn[35];
        string lowerOutput, lowerOutputSecond, twoColumn[35][2];



        do {
            cout << "Please enter a number between 1 and 5000: ";
            cin >> input;
            if (input >= 1 && input <= 5000) {
                error = 0;
                break;
            }
            else {
                cout << "Invalid input, try again." << endl;
                error = 1;
                cin.clear();
                cin.ignore(80, '\n');
            }
        } while (error == 1);

        for (int i = 0; i < 35; ++i)
        {
            oneColumn[i] = input;
            twoColumn[i][1] = toLongRoman(input);
            lowerOutput = twoColumn[i][1];
            lowerOutputSecond = toLower(lowerOutput);
            twoColumn[i][2] = lowerOutputSecond;

            input = input + 1;

        }


        for (int c = 0; c < 20; ++c) {
            cout << oneColumn[c] << "   " << twoColumn[c][1] << " " << 
            twoColumn[c][2] << endl;
        }


    }


    string toLongRoman(int a) {
        int  b, stringPos = 0;
        char romanString[100] = "0";

        while (a >= 1000) {
            b = a / 1000;
            if (b >= 1) {
                romanString[stringPos] = 'M';
                stringPos++;
                a = a - 1000;
            }
            else {
                break;
            }
        }

        while (a >= 500) {
            b = a / 500;
            if (b >= 1) {
                romanString[stringPos] = 'D';
                stringPos++;
                a = a - 500;
            }
            else {
                break;
            }
        }

        while (a >= 100) {
            b = a / 100;
            if (b >= 1) {
                romanString[stringPos] = 'C';
                stringPos++;
                a = a - 100;
            }
            else {
                break;
            }
        }

        while (a >= 50) {
            b = a / 50;
            if (b >= 1) {
                romanString[stringPos] = 'L';
                stringPos++;
                a = a - 50;
            }
            else {
                break;
            }
        }

        while (a >= 10) {
            b = a / 10;
            if (b >= 1) {
                romanString[stringPos] = 'X';
                stringPos++;
                a = a - 10;
            }
            else {
                break;
            }
        }

        while (a >= 5) {
            b = a / 5;
            if (b >= 1) {
                romanString[stringPos] = 'V';
                stringPos++;
                a = a - 5;
            }
            else {
                break;
            }
        }

        while (a >= 1) {
            b = a / 1;
            if (b >= 1) {
                romanString[stringPos] = 'I';
                stringPos++;
                a = a - 1;
            }
            else {
                break;

            }
        }

        return romanString;

    }

    string toLower(string s)
    {
        char result[50] = { 0 };

        for (unsigned int i = 0; i < s.length(); i++)
        {
            s[i] = tolower(s[i]);
            result[i] = s[i];

        }



        return result;
    }
c++
exception
asked on Stack Overflow Nov 24, 2018 by BaldDonkey

1 Answer

0

Arrays are 0 based, so twoColumn[i][2] = lowerOutputSecond is out of bounds in the second dimension - change the [2] to be [1] to be in bounds (and change the other [1] in your code to be [0]

answered on Stack Overflow Nov 24, 2018 by Ian4264

User contributions licensed under CC BY-SA 3.0