运行时检查失败#2 - 变量'ary'周围的堆栈已损坏。为什么?

时间:2014-11-29 00:04:41

标签: c++ compiler-warnings

我是新人,不知道我在做什么。 编译警告已打开,不显示任何警告。弹出可执行文件并发出运行时检查失败#2的警报。

为什么会发生这种情况,我们将不胜感激。

#include <iostream> 
#include <string>

using namespace std;

class romanType {
public:
    string strg;
    void inputRoman(int ary[]);
    //void CalculateRoman(int ary[]);
    //void outputRoman(int total);

};


int main()
{
    int M = 1000;
    int D = 500;
    int C = 100;
    int L = 50;
    int X = 10;
    int V = 5;
    int I = 1;


    romanType numerals; 
    int ary[50];
    cout << "This is to convert your input of Roman numerals to a positiver integer" << endl;
    cout << "When prompted, do as you're told" << endl;



    numerals.inputRoman(&ary[50]);
//  numerals.CalculateRoman(&input[50]);


    return 0; 
}   

void romanType::inputRoman(int ary[])
{
    string strg;
    int array_size;
    int i;

    cout << "Input the an appropriate Roman Numeral value" << endl;

    cin >> strg;
    array_size = strg.length();

    for (i = 0; i < array_size; i++)
    {
        ary[i] = strg[i];

    }


}

    /*
void romanType::CalculateRoman(int ary[])
{
    int total = 0;
    int i;

    for (i=0; i < 50 ; i++){

    if (ary[i] < (ary[i + 1])){
        total = total + (ary[i + 1] - ary[i]);
    }
    else {
        total = total + ary[i];
    }
    }

    cout << "Your conversion should equal " << total << endl;
}
*/`

1 个答案:

答案 0 :(得分:2)

&ary[50]ary的第51个元素的地址,这意味着它指向ary的最后一个元素之后。将其更改为ary

numerals.inputRoman(ary);