0

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!

Vince
  • 109
  • 2
  • 9
  • 1
    Can you post what `incomeInfo` is? Using a line like `incomeInfo[i].name` implies an array and what you are passing in doesn't look like an array or pointer. – user2205930 Sep 12 '20 at 02:26
  • A [mre] is highly recommended. – user4581301 Sep 12 '20 at 02:27
  • Hey, I added the structure declaration and the array initilization. – Vince Sep 12 '20 at 02:29
  • 1
    @Vince: `getData` receives a *single* `incomeInfo`, not an array (or pointer, or any indexable thing) of them. Did you mean to receive the whole array? You need a [MCVE]; we're missing how the function is called. – ShadowRanger Sep 12 '20 at 02:31

1 Answers1

1

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);
}
user2205930
  • 1,046
  • 12
  • 26
  • Thanks! never included the pointer so it wasn't working. Do we need to include an array pointer because C++ is Pass by Value? Why do we need the *? – Vince Sep 12 '20 at 02:59
  • 1
    `count` is passed by value and `incomeInfo` is passed by reference. A pointer `*` is pass by reference so any change you make will persist, i.e. in the `main` function you can print `employees` values to screen after the call to `GetData` and the values will be present. – user2205930 Sep 12 '20 at 05:32
  • 1
    @Vince Just about everything in C++ defaults to pass by value. The array is the notable exception. The array automatically [decays to a pointer](https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay). – user4581301 Sep 13 '20 at 03:20