Dear stackoverflow helpful community,
This is my first program using a pointer with a structure, and despite lots of research, I was not able to find what I was looking for. Please forgive me if this has already been responded to.
I have a project for school where I have to define structures than use pointers array to store data. In this loop, I get the following error :
Expression must have pointer-to-object type
for (int i = 0; i < nbClerk; i++)
{
cout<<"Number of hours: ";
cin>>c_info->hoursWorked[i];
}
break;
here's the whole code. thank you very much for your help
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
//structure defining Employee
struct Employee
{
int hoursWorked;
int hourRate;
double overtime;
string name;
int empID;
};
//Function collecting and calculating info
void employeeInfo(int nbOperator, int nbClerk){
char classOfEmployee;
Employee *c_info;
c_info = new (nothrow) Employee[nbClerk];
Employee *o_info;
o_info = new (nothrow) Employee[nbOperator];
cout<<"Select the class of employee (C=Clerk, O=Operator)";
cin>>classOfEmployee;
switch (tolower(classOfEmployee))
{
case 'c':
for (int i = 0; i < nbClerk; i++)
{
cout<<"Number of hours: ";
cin>>c_info->hoursWorked[i];
}
break;
}
}
int main(){
int nb1,nb2;
cout<<"Nb Employee and nb of nb of operator: ";
cin>>nb1>>nb2;
nbEmployee(nb1, nb2);
system("pause");
}
You probably meant:
c_info[i].hoursWorked;
since c_info
is an array, by doing c_info[i]
you'll access the i
-th instance (object) of Employee
class in c_info
array, and then obtain hoursWorked
through .
operator.
Now you can clearly see that your variant simply doesn't make sense, as hoursWorked
is just an integral type and not an array, and therefore you cannot apply []
operator to it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With