trouble with function calling

70 views Asked by At

I have three arrays: a string, an int, and a double. I need to output the contents of them by calling them from the main function to another function. When I try to build the program, there is an error saying "identifier not found" for displayEmployeeInformation. Here is my code:

#include <iostream>
#include <string> 
using namespace std;

const int numEmp = 5;
string names [numEmp];
int ages [numEmp];
double salaries [numEmp];

int main () {
    const int numEmp = 5;
    string names [numEmp];
    int ages [numEmp];
    double salaries [numEmp];

    for (int i = 0; i < numEmp; i++) {
        cout << "Enter Employee #" << i+1 << " Name: ";
        cin >> names [i];
        cout << "Enter Employee #" << i+1 << " Age: ";
        cin >> ages [i];
        cout << "Enter Employee #" << i+1 << " Salary: ";
        cin >> salaries [i];
    } 

    displayEmployeeInformation (names, ages, salaries, 5);

    getchar();
    getchar();

    return 0;
}

int displayEmployeeInformation (string names [], int ages [], double salaries [], int size) {
    for (int x = 0; x < numEmp; x++) {
        cout << "Employee #"  << x+1 << " Name: " << names [x] << ", Age: " << ages [x] << ", Salary: " << salaries [x];
    }

    return 0;
}
3

There are 3 answers

3
Bathsheba On

You need to add a function prototype for the function. Put this line

int displayEmployeeInformation (string names [], int ages [], double salaries [], int size);

above int main().

Alternatively, put the entire function definition above int main().

0
Andreas DM On

As already pointed out in a good answer by Bathsheba, place the function definition above main or declare a function prototype.

I also think that your function return type could be void as you're not doing anything with the value returned:

void displayEmployeeInformation(string names [], int ages [], 
                                double salaries [], int size) {
  for (int x = 0; x < numEmp; x++) {
    cout << "Employee #"  << x+1 << " Name: " << names [x] 
         << ", Age: " << ages [x] << ", Salary: " << salaries [x];
  }
}
0
Vlad from Moscow On

At first take into account that you defined arrays with the same names twice: in the global namespace and within the block scope of function main

//...
const int numEmp = 5;
string names [numEmp];
int ages [numEmp];
double salaries [numEmp];

int main () {
    const int numEmp = 5;
    string names [numEmp];
    int ages [numEmp];
    double salaries [numEmp];

/...

You may bravely remove definitions in the global namespace.:)

As for the function call then the function declaration shall appear before the function usage.

You may place the function declaration either before main or inside main but in any case before the call of the function. For example

int displayEmployeeInformation (string names [], int ages [], double salaries [], int size);
int main () {

Also the function could be decalred as having type void because the returned value has no great sense.

The function could be declared like

void displayEmployeeInformation ( const string names [], const int ages [], const double salaries [], int size);

ALso it would be better to define an array of a structure that would contain data members for name, age and salary.