罗马数字转换程序引发异常。新手问题

时间:2018-11-24 22:46:25

标签: c++ exception

异常是“在ConsoleApplication28.exe中的0x0F71514F(vcruntime140d.dll)处引发了异常:0xC0000005:发生访问冲突写入位置0xCCCCCCCC。”

这似乎与三行有关

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

在我的一生中,我无法弄清楚问题出在哪里。我可以删除这些行,尽管还不完整,但是行得通。任何帮助将不胜感激。

    #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;
    }

1 个答案:

答案 0 :(得分:0)

数组是基于0的,因此twoColumn [i] [2] = lowerOutputSecond在第二维上超出范围-将[2]更改为[1]以使其成为界限(并在其中更改其他[1]您的代码为[0]