this is a weird error

May 5, 2014 at 12:58pm
I am coming up with this error in my code and I dont know why. Can anyone help me and let me know whats going on?

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
 #include<iostream>
#include<cmath>
#include<cstdlib>
using namespace std;

int main() {
    double owen[5]={};
    populate(owen, 5)
    spit(owen, 5)
    
        return 0;
    
}

int populate (double array1[], int number ) {
    double input;
    int i;
    for(i=0;i<number;i++){
        cout<<"Enter value for item "<<(i+1)<<endl;
        cin>>input;
    array1[i]=input;
        
    
    }
return i;
}

void spit (double array1[], int number) {
    for (int i=0; i<number;i++) {
        cout<<"The value of item"<<(i+1)<< " Is "<<array1[i];
    }
}
May 5, 2014 at 12:59pm
The error is coming up in the populate commande it says use of undeclared identifier 'populate'
May 5, 2014 at 1:09pm
The reason why is you are getting a undeclared identifier error is because you are defining your function after the main() function. Remember that code runs top to bottom. In the example anything below line 13 the main function doesn't know exists unless you have a forward declaration before it or defined it before it.

For example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Forward declaration of the function
int populate(double array1[], int number);

int main()
{
    ...
    ...
}

// Actual definition of the function
int populate(double array1[], int number)
{
    ...
    ...
}


On line 8 and 9 you are missing semi colons to terminate the statements.

They should look like

1
2
    populate(owen, 5);
    spit(owen, 5);
Last edited on May 5, 2014 at 1:10pm
May 5, 2014 at 1:11pm
Thank you I just figured it out as you posted this. :) thank you though!
Topic archived. No new replies allowed.