0

after making a fresh start on a new program i made for learning how arrays work in combinatio0n with void ive ran into the following problem.

cpp(15): error C2182: 'input' : illegal use of type 'void'

Does anyone know what causes this? I am new to the concept of void and array.

#include "stdafx.h"
#include <iostream>
using namespace std;
void input (int x );

int main()
{
    int x = 0;
    int a[ 5 ];
    input ( a[ 5 ]);
    {
        void input(x);
        for(int i = 1; i < 5; i++) {
            cin >> a [ i ];
        }
        cin.get();
        cout << a [ 3 ];
        cin.get();
    }
}
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
user3383990
  • 1
  • 1
  • 2

2 Answers2

5

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.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
1

You're going to have more errors after resolving the current one. Here's some quick pointers that may help. I don't want to just give you a solution because you are still learning and that won't help:

  • void as a keyword refers to the "nothing type" and is used in functions to denote having no return value
  • curly braces {} denote scope and can be used to define the body of a function, loop, or control statement
  • Functions themselves need to be declared and defined. The definition, or body of the function, can be later on in your code but the declaration needs to be present before you call it

Here's an example program to illustrate basic function parts:

#include <iostream>

// declaration
void Welcome(); 

int main()
{
  // function call
  Welcome(); // displays "Hello World"
  return 0;
}

// definition
void Welcome()
{
  std::cout << "Hello World" << std::endl;
}

More on functions

As far as arrays they are basically a contiguous block of memory large enough to hold a given amount of the same type. Here's a few things to remember about arrays:

  • They work with integral types as well as objects but are usually used for plain old data. e.g. int intArray[5]; is an array of 5 int types.
  • The index starts at 0 meaning intArray[0] from previous example is the first integer.
  • Using the array operator you can get and set values e.g. int last = intArray[4]; or intArray[0] = -1;

More on arrays

Check out the other answers for more on how to pass arrays as parameters but I also recommend picking a Good C++ Book ;-)

Community
  • 1
  • 1
AJG85
  • 15,849
  • 13
  • 42
  • 50