samedi 11 juillet 2020

Missing integer when printing out from array after certain length

I have the following code:

#include <iostream>

using namespace std;

int main()
{
    int length = 0;
    int arrA[length];

    cout << "Enter the length : ";
    cin >> length;

    cout << "Enter " << length << " integers for array : ";

    for(int i = 0; i < length; i++)
    {
        cin >> arrA[i];
    }

    cout << "Array A : ";
    for(int i = 0; i < length; i++)
    {
        cout << arrA[i] << " ";
    }
    cout << endl;
}

The above require the user to input the length of array followed by the integers that will be store in array. It work but the printing of the array value is incorrect when I input length 8 and above.

Working

Enter the length : 7
Enter 7 integers for array : 7 6 5 4 3 2 1
Array A : 7 6 5 4 3 2 1

Not working

Enter the length : 8
Enter 8 integers for array : 8 7 6 5 4 3 2 1
Array A : 8

Could this be due to memory issue or something?

Aucun commentaire:

Enregistrer un commentaire