-1

I'm sorry, I know similar questions have been asked but I'm not understanding the replies. Usually something to do with pointers, which we haven't learned about in my class.

Here is the program

#include <iostream>

using namespace std;

const int NUM_EMPLOYEES = 4;

void readData(string name[NUM_EMPLOYEES], double salary[NUM_EMPLOYEES]);
void updateCOL(double salary[NUM_EMPLOYEES], double COL);
double payroll(double salary[NUM_EMPLOYEES]);

int main()
{
    string name[NUM_EMPLOYEES];
    double salary[NUM_EMPLOYEES];
    double COL;

    readData(name, salary);

    cout << "Cost of living?" << endl;
    cin >> COL;

    updateCOL(salary, COL);

    for (int i = 0; i < NUM_EMPLOYEES; i++)
    {
        cout << name[i] << " $" << salary[i] << endl;
    }

    cout << endl
         << "Total Payroll: $" << payroll(salary) << endl;
    return 0;
}

void readData(string name[NUM_EMPLOYEES], double salary[NUM_EMPLOYEES])
{
    for (int i = 0; i < NUM_EMPLOYEES; i++)
    {
        cout << "Name salary? ";
        cin >> name[i] >> salary[i];
        cout << endl;
    }
}

void updateCOL(double salary[NUM_EMPLOYEES], double COL)
{

    for (int i = 0; i < NUM_EMPLOYEES; i++)
    {
        salary[i] = salary[i] + (COL / 100);
    }
}

double payroll(double salary[NUM_EMPLOYEES])
{
    double payroll = 0;

    for (int i = 0; i < NUM_EMPLOYEES; i++)
    {
        payroll += salary[i];
    }

    return payroll;
}

Expected inputs and outputs are

Output: Name salary? 
Input: John 32000

Output: Name salary? 
Input:  Jack 56000

Output: Name salary? 
Input:  Jane 65000

Output: Name salary? 
Input:  Jill 50000

Output: Cost of living? 
Input:  2

Output: 
John $32640
Jack $57120
Jane $66300
Jill $51000

Total payroll: $207060

But when on the final output, I'm just getting the inputs back without being updated. Thanks for the help in advance!

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
  • 1
    With `COL` set to `2`, `updateCOL` adds `0.02` to the salary, I'm guessing this isn't what you intended and appears to have no effect due to the precision that you're printing the numbers at. Maybe you meant `salary[i] = salary[i] * (1 + COL / 100)` (assuming `COL` is supposed to be a percentage)? Also see https://stackoverflow.com/questions/588004/is-floating-point-math-broken for why you shouldn't be using floating point for dealing with money – Alan Birtles Oct 30 '22 at 22:09
  • @Tinashe Whatever you are trying to say here, it's incorrect. There's nothing wrong with the variable scope in this program. – paddy Oct 30 '22 at 22:13
  • As @AlanBirtles said, the formula is incorrect. The values do change, but by a tiny amount. https://godbolt.org/z/conE5qoP8 – Retired Ninja Oct 30 '22 at 22:14

1 Answers1

-1

Thank you guys for your help, it totally was my formula lmao.