Array

I am unable to find my mistake, and i don't know what I am doing incorrectly. I can't get right the element part. I need to do a Load 10 integers into an array, then prompt the user for a number from 0 to 9. Display the element at the array position the user selects. For example:

Reading in: 77, 33, 88, 99, 22, 55, 11, 44, 66, 0
Enter a number from 0 to 9: 3
Element number 3 is 99
my code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
using namespace std;
int main() {
int numbers[10];
int i;

   
cout<<"Reading in: " ;
for(int i=0; i<10; i++){
cin>>numbers[i];
}
for(int i=0;i<9;i++) 
cout<<numbers[i]<<","<<" "; 
cout<<numbers[9]<<endl;

cout<<"Enter a number from 0 to 9: ";
cin>>numbers[9];
cout<<numbers[9]<<endl;

if(numbers[9]>9)
{
cout<<"Number is out of range."<<endl;
}
else {
cout<< "Element number ";
cin>>numbers[9];
cout<<numbers[9];
cout<< " is ";
cin>> numbers[i];
cout<<numbers[i];
}

return 0;
}

Please help thank you in advance :)
Last edited on
Why do you overwrite the last element of the array with the input that you will use as an index?

You should use a different variable for that input.
Hello lescarys,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

If you want your output to match your example then you have codded the program wrong.

Starting from the line cout<<"Enter a number from 0 to 9: "; and after the code needs to be reworked to match your example output.

What is the purpose of cin>>numbers[9];? I mean the one above the if statement. The "cin" here should be getting proper value for "i' to be used in the else block and there should be no input in the else block.

And in the else block the line cin>> numbers[i]; "i" has no usable value at this point.

Hope that helps,

Andy
hi Andy!
yes you are right :) i saw the light on the dark tunnel, i reviewed the code and i got it thank you!!!
Hello lescarys,

Your are welcome. Anything else let me know.

Andy
Topic archived. No new replies allowed.