c++ help

every time i try to run my program i keep getting an identification error for empInfo. i tried putting int empInfo; but it wont work can someone please help me out. thank you in advance for your help


#include <stdlib.h>
#include <iostream>
using namespace std;

class employee
{
private:
int id, maxHours;
float payRate;

public:
void setInfo (int pin, float pay, int hours)
{
empInfo[pin].id = pin;
empInfo[pin].payRate = pay;
empInfo[pin].maxHours = hours;
}

void viewInfo (int record)
{
cout << empInfo[record].id;
cout << endl << empInfo[record].payRate;
cout << endl << empInfo[record].maxHours;
}

};
int main ( )
{
int pin, hours, record;
float pay;

employee empInfo[1000];

cout << "Enter employee 3 digit ID: ";
cin >> pin;
cout << "Enter employee pay rate: ";
cin >> pay;
cout << "Enter employee max hours: ";
cin >> hours;

empInfo[pin].setData(pin, pay, hours);

cout << "Enter the employee ID# of the record you wish to view: " << endl;
cin >> record;

empInfo.viewDate(record);

system("PAUSE");
return 0;
}
Well first your problem is in your functions inside of the employee class, you have

1
2
3
4
5
6
void setInfo (int pin, float pay, int hours)
{
empInfo[pin].id = pin;
empInfo[pin].payRate = pay;
empInfo[pin].maxHours = hours;
}

empInfo is something you declared in your main function, so there is no way for the employee class to know what that is since it isn't in the same scope.

When you call empInfo[pin].setInfo(pin, pay, hours); inside your main you're calling the setInfo function inside your employee class FOR the object empInfo[pin], so the attributes and functions in the employee class are only working for that particular element in the array at this point.

An example, lets pretend I have a Phone class and I'm setting up some objects for it:
1
2
3
4
5
    Phone droid; 
    Phone iphone;

    iphone.setInfo("3G", "White"); // Call 1
    droid.setInfo("4G", "Black");    // Call 2 

When I get to Call 1, everything that happens inside of that call inside of my Phone class, only pertains to iphone.
Everything that happens inside of the Phone class for Call 2, only deals with the droid object.


So for your functions you can just have
1
2
3
4
5
6
void setInfo (int pin, float pay, int hours)
{
    id = pin;
    payRate = pay;
    maxHours = hours;
}


And the same goes for your other function
1
2
3
4
5
6
void viewInfo () // Took out the parameter, will explain down below
{
    cout << id;
    cout << endl << payRate;
    cout << endl << maxHours;
}




Now the problems in your main function, you probably changed some names around and forgot to change these but
 
empInfo[pin].setData(pin, pay, hours);

should be
 
empInfo[pin].setInfo(pin, pay, hours);


Same thing for
 
empInfo.viewDate(record);

should be
 
empInfo[record].viewInfo();


I took out the int record parameter from the viewInfo() function since your array is in the main function, so you can just access the specific element from there, then call the viewInfo() method from it
Last edited on
empinfo is the name of an array, defined in function main().

employee empInfo[1000];

You don't need to refer to this array from inside your class. Remove all references to empInfo from member functions setInfo() and viewInfo().
Topic archived. No new replies allowed.