Your code has many problems with it. It's just not valid C++ as it is. Remember that C++, like any other programming language, is unforgiving when it comes to syntax. If it's not exactly the right syntax, it's not going to compile. You can't just write what you think makes sense. You need to learn the correct syntax and apply it.
It looks like you want everything from the for
loop to the last cin.get()
to be part of a function called input
. To do that, you need to use the appropriate syntax for defining a function and you need to do it outside any other functions:
void input(int x) {
for(int i = 1; i < 5; i++) {
cin >> a [ i ];
}
cin.get();
cout << a [ 3 ];
cin.get();
}
This still has a problem though. The parameter type is int
, yet it looks like you want to pass the entire array:
void input(int x[])
Note that this is not actually an array type parameter, but is really a pointer. When you pass an array to this function, x
will be a pointer to its first element. The []
is just a convenient syntax.
Then, instead of passing a[5]
to the function (which is an element that does not exist, since only a[0]
to a[4]
exist), you should be passing just a
:
input(a);
You also loop from 1
to 4
- I'm not sure if this is intentional. If you want to input a value for each element of the array, you should be looping from 0
to 4
.