-2

私は C++ を学んでおり、ユーザーが 10 個の整数を含む配列を変更できるようにするプログラムを作成するタスクを与えられました。ユーザーが範囲外のインデックスを指定すると、プログラムは終了します。プログラムは、負の数と範囲内のすべての数で動作します。範囲を超える 10 のような数値を入力すると、次のようになります。

* スタック破壊が検出されました * : 終了しました

私はこれが初めてで、どんな助けでも大歓迎です。

#include <iostream>
#include <array>
using namespace std;

int main()
{
    array<int, 10> myData; // creates array size 10
    int i = 0;
    int v = 0;

    for (unsigned int n = 0; n < myData.size(); n++) // makes all elements 1
    {
        myData[n] = 1;
    }

    do
    {
        for (unsigned int a = 0; a < myData.size(); a++)
        {
            cout << myData[a] << " ";
        }
        cout << endl << "Input index: ";
        cin >> i;
        cout << endl << "Input value: ";
        cin >> v;
        myData[i] = v;
    } while (i >= 0 && i < myData.size());
    {
        cout << endl << "Index out of range: Exit " << endl;
    }
    return 0;
}

プログラムを実行すると、次のようになります。

1 1 1 1 1 1 1 1 1 1
Input index: 10

Input value: 4

Index out of range: Exit
*** stack smashing detected ***: <unknown> terminated
[1]    56 abort (core dumped)  ./edit
4

1 に答える 1