Error filling a vector and displaying each of its positions (c ++) [closed]

0

I have an idea but it produces an error, and when I finish writing the numbers entered in the console, the numbers do not come out after I find the number "1" in the vector, it may seem simple, but I have not been able to solve it (unless it is the IDE or the pc)

Thanks

int vector[50];

cout << "Escriba el Numero" << endl;
for (int i = 0;i <= 50;i++)
{
    cin >> vector[i];
    if (vector[i] ==-1)
    {
        break;
    }

}


cout << "El Numero Completo Es: " << endl;
for (int i = 0; i <= vector[i]; i++)
{
    cout << vector[i];
}
cout << "?" << endl;
    
asked by devmeg 24.08.2018 в 00:32
source

1 answer

0

I guess you mean this error:

Where you can see that evidently, since the 1 stops showing the elements of the vector.

That is fixed by changing the conditional of the second for like this:

int vector[50];

    cout << "Escriba el Numero" << endl;
    for (int i = 0;i <= 50;i++)
    {
        cin >> vector[i];
        if (vector[i] == -1)
        {
            break;
        }

    }


    cout << "El Numero Completo Es: " << endl;
    for (int i = 0; -1 != vector[i]; i++) //<<--aquí cambiamos la
    {                                     //condicional para que se detenga
        cout << vector[i];                //cuando encuentre un -1
    }
    cout << "?" << endl;

As you can see, the error does not happen again and it shows all the elements within vector

    
answered by 24.08.2018 / 05:29
source