Vince
Vince

Reputation: 136

C++ No operator [] matches these operands

Sorry if this is a noob mistake, I'm really new to C++. My cin is not taking the value I'm trying to pass it.

void getData(incomeInfo incomeInfo, const int NUM_EMPS) {

for (int i = 0; i < NUM_EMPS; i++) {
    cout << "Employee #" << i + 1 << "'s name: " << endl;
    cin >> incomeInfo[i].name;
    cout << endl;
    cin.ignore();
}

The incomeInfo structure:

struct incomeInfo {
string name;
double pay;
double healthInsuranceDeduction;
};

And the call:

incomeInfo employees[NUM_EMPS];

The error message I get is No operator [] matches these operands; operands types are incomeInfo[int]. I'm passing it an int. Thanks!

Upvotes: 0

Views: 3964

Answers (1)

user2205930
user2205930

Reputation: 1086

You're declaring your function wrong, you need an array or pointer and incomeInfo is just a structure so you cannot use incomeInfo[i].name. Here's something that should work, pay attention to the upper and lower case names:

struct IncomeInfo
{
   string name;
   double pay;
   double healthInsuranceDeduction;
};

void GetData(IncomeInfo* incomeInfo, const int count)
{
   for (int i = 0; i < count; i++)
   {
      cout << "Employee #" << i + 1 << "'s name: " << endl;
      cin >> incomeInfo[i].name;
      cout << endl;
      cin.ignore();
   }
}

void main()
{
   IncomeInfo employees[NUM_EMPS];

   GetData(employees, NUM_EMPS);
}

Upvotes: 1

Related Questions