cin stream not closing????


so for some reason that i do not understand this code below(a void function)
looks as if it should work fine but it always replaces a[9] with what ever v is.

perhaps for some reason the cin stream is not closing and its sticking the value of v into a[9] but i tried using a cin.clear after the array plug in loop but no difference

i wrote the code a different way that works
(which i have added to the bottom),,

but i would like to understand my error with this current code ,, if any one can point me in the right direction or a help hint it would much appreciated ,, thanks


/// code that doesnt work
void exerTwo()
{
/// type 10 integers of an array and an integer V. The program must search if V is in the array of 10 integers.
/// The program writes "V is in the array" or "V is not in the array".

int a[9],number = 0, v = 0, x = 0;

cout << "enter 10 numbers and then one for V, ill tell you if V is in the list";
for(int i = 0;i<10;i++)
{
cout << "\nEnter a number :";
cin >> number;
a[i] = number;
}


cout << "\n Enter a number for V :";
cin >> v;

for(int i = 0;i< 10;i++)
{
cout << "\n test index a[" << i << "] is " << a[i] << "";
}

for(int i = 0;i<10;i++)
{
if(a[i] == v)
{
x++;
cout << "index " << i << " is " << a[i] << "";
}
}

if(x>0)
{
cout << "\nV is in the list";
}

if(x== 0)
{
cout << "V is not in the list";
}
}

/// code that works

void exerTwo()
{

/// type 10 integers of an array and an integer V. The program must search if V is in the array of 10 integers.
/// The program writes "V is in the array" or "V is not in the array".

int a[10], v = 0, x = 0;

cout << "enter 10 numbers and then one for V, ill tell you if V is in the list";
for(int i = 0;i<11;i++)
{
if(i <10)
{
cout << "\nEnter a number :";
}
else
{
cout << "Enter a number for V :";
}

cin >> a[i];

}

for(int i = 0;i<10;i++)
{
if(a[i] == v)
{
x++;
}
}

if(x>0)
{
cout << "\nV is in the list";
}

if(x== 0)
{
cout << "V is not in the list";
}
}




Valid indexes for an array with 9 elements are 0-8. You are accessing outside the bounds of the array in your loops.

Note that, although your comments say you have a 10 element arrray, you only have a 9 element array.
Last edited on
are you talking about when its running the for loops,
the final iteration tries to access a[10] ,
falis and moves on

do you mean that for out of bounds accessing?


a[9] would allow for 10 elements though...
Last edited on
a[9] would allow for 10 elements though...

Assuming you're referring to int a[9], no - it would not. 9 is the number of elements in the array.

http://ideone.com/5YyL8M
Last edited on
it starts at zero(0,1,2,3,4,5,6,7,8,9) == # of indexes
1,2,3,4,5,6,7,8,9,10 == # of elements
a[9] == 10 elements..
at least that the way i understand it,,
it starts at zero(0,1,2,3,4,5,6,7,8,9) == # of indexes

Yes, it does start at 0.

(0,1,2,3,4,5,6,7,8) == # of indexes.
(1,2,3,4,5,6,7,8,9) == # of elements
a[9] == 9 elements..

http://ideone.com/5YyL8M
that doesnt add up..

the first would be a[8]
having 9 elements...

so a[9] should have 10 elements

the number of elements = # of indexs +1

a[0] = 1 element
a[1] = 2 element
a[2] = 3 element
a[3] = 4 element

etc, etc
a[9] = 10 elements

to be honest i dont know much about the memory aspect of it ,, but
the two dont add up

http://www.cplusplus.com/doc/tutorial/arrays/

im not trying to be a smartass but the thing above that i typed and that link would both agree that a[9] = 10 elements
im not trying to be a smartass but the thing above that i typed and that link would both agree that a[9] = 10 elements

In the first few paragraphs in the link you gave, it shows that the valid indexes for an array of size 5 is 0 to 4. Indexes = { 0, 1, 2, 3, 4 } which is 5 elements. Notice that the last index (4) is one less than the size (5). You have an array of size 9. One less than 9 is 8. Valid indexes for your array are 0 to 8, which is 9 values. So, no... you don't agree with the link you posted.

Furthermore, the text of the tutorial says this:
Therefore, the foo array, with five elements of type int, can be declared as:

int foo [5];


The number of elements is 5, not 6.

If we had written int foo[9];, the number of elements would be 9, not 10.
Last edited on
Array index starts at 0 and finishes at array size - 1. That means the array index starts at a[0] and finishes at a[8].

a[0] = 1 element
a[1] = 2 element
a[2] = 3 element
a[3] = 4 element


a[4] = 5 elements
a[5] = 6 elements
a[6] = 7 elements
a[7] = 8 elements
a[8] = 9 elements

so a[4] {0,1,2,3,4}
1,2,3,4,5 -- counting the elements-- = 5

In the first few paragraphs in the link you gave, it shows that the valid indexes for an ((((array of size 5 is 0 to 4. Indexes = { 0, 1, 2, 3, 4 } which is 5 elements)))). Notice that the last index (4) is one less than the size (5). You have an array of size 9. One less than 9 is 8. Valid indexes for your array are 0 to 8, which is 9 values. So, no... you don't agree with the link you posted.

i dont agree with the text and
i dont understand it honestly

i dont get how if you declare an array as a[4] will give 5 elements ( the link showed it as declared as a[5]

a[4] {0,1,2,3,4}
1,2,3,4,5 -- counting the elements-- = 5 elements
so wouldnt a[5] {0,1,2,3,4,5}
1 2 3 4 5 6 = 6 elements








@yatora ,, so to declare that array, you would do it as a[8]; right??
This gives a compiler error:
 
    int a[4] {0,1,2,3,4};

[Error] too many initializers for 'int [4]'


The error can be fixed as either
 
    int a[4] {0,1,2,3};   // four elements, four initialisers 

or
 
    int a[5] {0,1,2,3,4}; // five elements, five initialisers 
that makes sense ok lol thanks for your patience
the way i took it from books and online sources the a[4] ,, the 4 would the number of the last index able access with out an out of bounds happening
I think there is possibly confusion arising because if we write a[4] it can mean two completely different things, depending on context.

1. define an array and allocate storage for it:
 
    int  a[4];

2. access the contents of an individual element:
 
    cout << a[4];


The number 4 here means different things depending on context. Example 1 it is the array size, which is four. Example 2 it is the subscript (or index) of individual element number five (error, outside the array here).
Last edited on
yea thats exactly what i was getting confused on ,,
Topic archived. No new replies allowed.