array as parameters

May 23, 2016 at 7:38pm
hi guys I'm just wondering why this code won't work I get an error when I try to pass in an array,I set the parameters to take an array yet I get an error I'm not sure why? fillArray(int arrayy[30]); is where I get the error.

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
  
#include <iostream>

using namespace std;


const int MAX = 20;
int numberOfStudents = 0;
int main()
{

  fillArray(int arrayy[30]);

}

void fillArray(int x[]){

    int output;
    int i;
    cout << "enter scores of students type -1 to quit";
    cin >> output;
    while(output != -1){

        cin >> output;
        numberOfStudents++;
        x[i];
        i++;
    }
}

May 23, 2016 at 7:52pm
You are trying to do the equivalent of this:
1
2
3
4
5
6
7
int main() {
    f(int x + 2); // do you see why this is wrong?
}

void f(int i) {
    // ...
}
May 23, 2016 at 7:56pm
The compiler has to know about functions before you try to use them. When you get to line 12, the compiler has no idea what the function fillArray is.

Also, on line 12, exactly what are you trying to pass to the function? Ugh, this is just a big mess.

Stop using arrays. Arrays are for advanced programmers. Beginners should use vectors.

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
#include <iostream>

using namespace std;
const int MAX = 20;
int numberOfStudents = 0;


void fillArray(int x[]){

    int output;
    int i;
    cout << "enter scores of students type -1 to quit";
    cin >> output;
    while(output != -1){
        cin >> output;
        numberOfStudents++;
        x[i];
        i++;
    }
}

int main()
{
  int arrayy[30];
  fillArray(arrayy);
}
May 23, 2016 at 8:27pm
Thanks I'll look into vectors
May 23, 2016 at 8:46pm
l
Last edited on May 23, 2016 at 8:53pm
May 23, 2016 at 8:52pm
You can't create a function inside another function. You're trying to create the function printArray inside the function fillArray.
May 23, 2016 at 9:15pm
yeah I realized that lol I think I'm going to call it a day today
Topic archived. No new replies allowed.