Simple array dump

Thread Starter

nanobyte

Joined May 26, 2004
120
I am trying to make a simple program that does the following:

Program asks an user for 8 inputs, takes each input and put it into an array and then outputs all the elements of the array.

But for some reason the program won't stopping taking taking inputs until I hit enter and then spits out garbage.

Rich (BB code):
int main()
{
    /* Program asks an user for an input and put it to an array
    and then list all the elements of the array.
    */
    cout<<"Enter 8 characters:"<<endl;
    int x;
    int array[8];
    for(x=0; x<=8; x++)
    {
        cin>>array[x];
    }
/*
    cout<<"The listed elements are:"<<endl;
    for(x=0; x<8; x++)
    {
        cout<<array[x]<< endl;
    }*/
    return 0;
}
 

bretm

Joined Feb 6, 2012
152
You're trying to read 9 elements (0 through 8) but the array is only 8 elements. And I think the ">>" operator on "cin" requires pressing Enter, but it's been a while.
 
Top