Simple array dump

Discussion in 'Programmer's Corner' started by nanobyte, Aug 1, 2012.

  1. nanobyte

    Thread Starter Senior Member

    May 26, 2004
    118
    1
    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.

    Code ( (Unknown Language)):
    1.  
    2. int main()
    3. {
    4.     /* Program asks an user for an input and put it to an array
    5.     and then list all the elements of the array.
    6.     */
    7.     cout<<"Enter 8 characters:"<<endl;
    8.     int x;
    9.     int array[8];
    10.     for(x=0; x<=8; x++)
    11.     {
    12.         cin>>array[x];
    13.     }
    14. /*
    15.     cout<<"The listed elements are:"<<endl;
    16.     for(x=0; x<8; x++)
    17.     {
    18.         cout<<array[x]<< endl;
    19.     }*/
    20.     return 0;
    21. }
    22.  
     
  2. JohnInTX

    Moderator

    Jun 26, 2012
    2,347
    1,029
    You maybe need an extra element in the array for the \0 to terminate the array?
     
  3. bretm

    Member

    Feb 6, 2012
    152
    24
    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.
     
Loading...