trouble with arrays and functions

Hello everyone

I'm trying to write a program that uses switch statements with arrays and function calls. I've written the program but am getting an error saying "undefined reference to ..." I thought that I had defined it in main() but I guess that isn't the case. Not sure what to do, if anyone can give me a clue or point me in the right direction that would be great. Thanks.

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  #include <iostream>
#include <iomanip>
using namespace std;

const int NAMELEN = 65;
const int SIZE = 20;

void printMenu();
void addEmployees(char[][NAMELEN], double[], double[], int&);
void displayAllEmployees(char[][NAMELEN], double[], double[], int);


int main()
{
    int count=0, choice;
    char name[SIZE][NAMELEN];
    double rate[SIZE], hours[SIZE];

    do{
        printMenu();
        cin >> choice;
        cout << endl;

        switch(choice)
        {
            case 1:
                addEmployees(name, rate, hours, count);
                break;
            case 2:
                displayAllEmployees(name, rate, hours, count);
            case 0:
                cout << "Goodbye. " << endl << endl;
                break;
            default:
                cout << "Invalid menu choice. Please enter valid ";
cout << "menu choice ";
                cin >> choice;
        }

    }while(choice!=0);

    return(0);
}

void printMenu()
{
    cout << endl;
    cout << "Welcome to the Employee Paycheck Calculator " << endl;
    cout << "=========================================== " << endl;
    cout << "1.  Add employee record(s) to the database " << endl;
    cout << "2.  Print all employee records " << endl;
    cout << "0.  Exit " << endl;
    cout << "=========================================== " << endl;
    cout << "Enter selection:  ";
}

void addEmployees(char name[], double rate[], double hours[], int count)
{
    cout << "How many employees do you wish to add? ";
    cin >> count;

    while (count < 1 || count > 65)
    {
        cout << "That is an invalid size. ";
        cout << "You must enter a value between 1 and 100. " << endl;
        cout << "How many employees do you wish to add? ";
        cin >> count;
    }

    for (int i = 0; i < count; i++)
    {
                                  cout << endl;
        cout << "Enter Employee " << i << " name: ";
        cin >> name[i];

        cout << "Enter " << name[i] << "'s pay rate: ";
        cin >> rate[i];

        cout << "Enter " << name[i] << "'s weekly hours: ";
        cin >> hours[i];
    }
}

                              39,1-8        76%
Last edited on
You declare addEmployees as
void addEmployees(char[][NAMELEN], double[], double[], int&);
but your definition of it is
void addEmployees(char name[], double rate[], double hours[], int count)
which doesn't match up, which is why you're getting the "undefined reference" error.

Also, you don't have your displayAllEmployees function defined (or you do, but you left it out when copy/pasting it into your post; if that's the case, then double-check to make sure that function's definition matches its prototype as well).
long double main replied before I could, but I fixed the code according to what was wrong (which is what he pointed out).
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  #include <iostream>
#include <iomanip>
using namespace std;

const int NAMELEN = 65;
const int SIZE = 20;

void printMenu();
void addEmployees(char[][NAMELEN], double[], double[], int&);
//void displayAllEmployees(char[][NAMELEN], double[], double[], int);


int main()
{
    int count=0, choice;
    char name[SIZE][NAMELEN];
    double rate[SIZE], hours[SIZE];

    do{
        printMenu();
        cin >> choice;
        cout << endl;

        switch(choice)
        {
            case 1:
                addEmployees(name, rate, hours, count);
                break;
            case 2:
                //displayAllEmployees(name, rate, hours, count);
            case 0:
                cout << "Goodbye. " << endl << endl;
                break;
            default:
                cout << "Invalid menu choice. Please enter valid ";
cout << "menu choice ";
                cin >> choice;
        }

    }while(choice!=0);

    return(0);
}

void printMenu()
{
    cout << endl;
    cout << "Welcome to the Employee Paycheck Calculator " << endl;
    cout << "=========================================== " << endl;
    cout << "1.  Add employee record(s) to the database " << endl;
    cout << "2.  Print all employee records " << endl;
    cout << "0.  Exit " << endl;
    cout << "=========================================== " << endl;
    cout << "Enter selection:  ";
}

void addEmployees(char name[][NAMELEN], double rate[], double hours[], int &count)
{
    cout << "How many employees do you wish to add? ";
    cin >> count;

    while (count < 1 || count > 65)
    {
        cout << "That is an invalid size. ";
        cout << "You must enter a value between 1 and 100. " << endl;
        cout << "How many employees do you wish to add? ";
        cin >> count;
    }

    for (int i = 0; i < count; i++)
    {
                                  cout << endl;
        cout << "Enter Employee " << i << " name: ";
        cin >> name[i];

        cout << "Enter " << name[i] << "'s pay rate: ";
        cin >> rate[i];

        cout << "Enter " << name[i] << "'s weekly hours: ";
        cin >> hours[i];
    }
}

Also, your employee name only takes the first name, trying to do first and last crashes the program sending it into an infinite screen scroll.
okay so i changed the declaration and definition so they match up. thanks. It was starting to drive me crazy. thanks again

Topic archived. No new replies allowed.