so we are starting arrays and our task is to go back to our assignment 2 (functions) and he wants us to run the program using arrays.

im pretty stuck and have no clue what to do.

heres how assignment (functions)2 looks

#include <iostream>
using namespace std;

void getEmployeeData (int*, char*, int*);
float calculateGrossPay(char, int);
bool isValidStatus(char);
//bool getEmployeeData(int);


int main()
{
	int pId;
	char pType;
	int pAmount;
	getEmployeeData (&pId, &pType, &pAmount);
	float cost = calculateGrossPay(pType, pAmount);
	if (pType == 'S' || pType == 's')
	{
		cout <<"Employee ID: " << pId << endl;
		cout <<"Payroll Status: " << pType <<"alaried" << endl;
		cout << "Gross Pay: " << '$' << cost << endl;
	}
	else if (pType == 'H' || pType == 'h')
	{
		cout <<"Employee ID: " << pId << endl;
		cout <<"Payroll Status: " << pType <<"ourly" << endl;
		cout << "Gross Pay: " << '$' << cost << endl;
	}
	else if (pType == 'C' || pType == 'c')
	{
		cout <<"Employee ID: " << pId << endl;
		cout <<"Payroll Status: " << pType <<"omissioned" << endl;
		cout << "Gross Pay:$ " << '$' << cost << endl;
	}

	system("pause");
	return 0;
}



void getEmployeeData (int* id, char* type, int* amount)
{

	bool result;
	do {
	cout <<"Enter Employee ID :";
	cin >> *id;
	} while (*id <= 0);
	do {
	cout <<"Enter Payroll Status :";
	cin >> *type;
	result = isValidStatus(*type);
	} while (result == false);
	if (*type == 'S' || *type == 's')
		{
			cout <<"Enter monthly salary :";
			cin >> *amount;
		}
	else if (*type == 'H' || *type == 'h')
		{
			cout <<"Enter number of hours worked this month :";
			cin >> *amount;
		}
	else if(*type == 'C' || *type == 'c')
		{
		cout <<"Enter total sales for month :";
		cin >> *amount;
		}
}

float calculateGrossPay(char c, int s)
{
	float f;

	if (c == 'S' || c == 's')
		f = s + 0;

	else if (c == 'H' || c == 'h')
		f = 18.75 * s;

	else 
		f = 1000 + s ;

	return f;
}

bool isValidStatus(char c)
{

	if (c == 'S' || c == 's' || c == 'H' || c == 'h' || c == 'C' || c == 'c')
		return true;
	else 
	{
		cout <<"Re-enter: 's' or 'S' for salaried, 'h' or 'H' for hourly,'c' or 'C' for commissioned :";
		return false;
	}
}

our new assignment is run this program with arrays.

this is a sample run of how it should look like

Enter employee ID ====> 123

Enter payroll status (s/h/c)====> s

Enter monthly salary ====> 5280

Enter employee ID ====> 111

Enter payroll status (s/h/c)====> H

Enter number of hours worked this month ====> 80.5

Enter employee ID ====> 999

Enter payroll status (s/h/c)====> c

Enter total sales for this month ====> 15750.00

Enter employee ID ====> -1

Employee ID Payroll Status Gross Pay

123 Salaried $5280.00

111 Hourly $1509.38

999 Commissioned $1945.00


this is what i got so far

#include <iostream> 
using namespace std; 
void getEmployeeData(int [ ], int);
void displayEmployeeInfo (int [ ], int);

int main() 
{ 
int stoodunce[4];
getEmployeeData(stoodunce, 4);

displayEmployeeInfo(stoodunce, 4);
cout << endl;
system("pause");
return 0; 
} 

void getEmployeeData(int id[ ], int size)
{
for (int x = 0; x < size; x++)
{
cout << "Enter Employee ID : ";
cin >> id[x];
}
}
void displayEmployeeInfo (int val[ ], int size)
{
for (int x = 0; x < size; x++)
cout << "Employee ID:" << 
val[x] << endl;
}

the next thing to do is payroll status thats the part im stuck on.

btw some hints

getEmployeeData - This function is called once from main and stores in the first three arrays the values inputted by the user for the employee ID, payroll status and either the input salary, hours worked, or monthly sales (as determined by the payroll status). This function ends when the user has inputted data for 4 employees or inputs a negative employee ID to stop input, whichever comes first. It returns to main the number of employees (up to 4 but could be less) that were stored in the arrays

The first array will hold the employee 3 digit ID. You can assume that the user entered a valid whole number. However, you cannot assume a 3 digit number will be entered. You must check to make sure that the user did not enter more or less digits than 3. (You don't need to check for a negative number since that ends input, as explained with getEmployeeData)

The second array will store the payroll status.

The third array will store either the input salary, hours worked, or monthly sales (as determined by the payroll status). Again there is no error-checking; you may assume the user enters valid values.

The fourth array will store the calculated gross pay.

idk if im doing it right or not, please help me.

Recommended Answers

All 14 Replies

You need to define some arrays first. Then we might be able to help you.

You also need to format your code so we can follow the code. Don't instructors teach proper formatting techniques these days?

commented: You've got a point here :) +3

hmm how do i fix this error "cannot convert paramater int fron char"

#include <iostream> 
using namespace std; 
void getEmployeeData(int [ ], char [ ], int);
void displayEmployeeInfo (int [ ], int);

int main() 
{ 
int stoodunce[4];
getEmployeeData(stoodunce, stoodunce,  4);

displayEmployeeInfo(stoodunce, 4);
cout << endl;
system("pause");
return 0; 
} 

void getEmployeeData(int id[ ], char ps[ ], int size)
{
for (int x = 0; x < size; x++)
{
cout << "Enter Employee ID : ";
cin >> id[x];
cout <<"Enter Payroll Status (s/h/c):";
cin >> ps[x];
}
}
void displayEmployeeInfo (int val[ ], int size)
{
for (int x = 0; x < size; x++)
cout << "Employee ID:" << 
val[x] << endl;
}

right now im in the payroll status. if i put "int ps [ ]" it runs but i need to input either s/h/c and obviously thats char so i thought just putting "char ps [ ]" would work but it didnt ehhh.

sample run of how this program works

Enter employee ID ====> 123
Enter payroll status (s/h/c)====> s
Enter monthly salary ====> 5280

Enter employee ID ====> 111
Enter payroll status (s/h/c)====> H
Enter number of hours worked this month ====> 80.5  

Enter employee ID ====> 999
Enter payroll status (s/h/c)====> c
Enter total sales for this month ====> 15750.00

Enter employee ID ====> -1

output
Employee ID      Payroll Status     Gross Pay

123              Salaried           $5280.00

111              Hourly             $1509.38

999              Commissioned       $1945.00

lol nvm i got it to work.

i added

int stoodunce[4];
char stoodunce1[4]

getEmployeeData(stoodunce, stoodunce1, 4);

if i get stuck ill be back =P

commented: I like people who don't let others do things for them, but try do do it theirself first :) +3

well im stuck in thid part. dont have a clue if im on the right part to getting a succesful build, please help me.

#include <iostream> 
using namespace std; 
void getEmployeeData(int [ ], char [ ], int [ ], int);
void displayEmployeeInfo (int [ ], char [ ], int);

int main() 
{ 
int stoodunce[4]; 
char stoodunce1[4];
int stoodunce2[4];
getEmployeeData(stoodunce, stoodunce1 , stoodunce2,  4);
displayEmployeeInfo(stoodunce, stoodunce1, 4);
cout << endl;
system("pause");
return 0; 
} 

void getEmployeeData(int id[ ], char ps[ ], int amount[ ], int size)
{
for (int x = 0; x < size; x++)
{
cout << "Enter Employee ID : ";
cin >> id[x];
cout <<"Enter Payroll Status (s/h/c):";
cin >> ps[x];
[B]if (ps[x] == 'S' || ps[x] == 's')
{
	cout <<"Enter Monthly Salary: ";
	cin >> amount[x];
}
if (ps[x] == 'H' || ps[x] == 'h')
	cout <<"Enter number of hours worked this month";
	cin >> amount[x];
}
if (ps[x] == 'C' || ps[x] == 'c')
{
	cout <<"Enter total sales for month:";
	cin >> amount[x];
}[/B]

}

void displayEmployeeInfo (int val[ ],char val1[ ], int size)
{
for (int x = 0; x < size; x++)
cout << "Employee ID:" << val[x] << " " << "Payroll" << val1[x] << endl;
}

sample run

Enter employee ID ====> 123
Enter payroll status (s/h/c)====> s
Enter monthly salary ====> 5280

Enter employee ID ====> 111
Enter payroll status (s/h/c)====> H
Enter number of hours worked this month ====> 80.5

Enter employee ID ====> 999
Enter payroll status (s/h/c)====> c
Enter total sales for this month ====> 15750.00

So what's wrong? The sample is doing exactly what the code says to do.

im not getting a sucessful build.

We are not psychic. When you take your car into the shop to get fixed, you need to give details to the mechanic so they have a clue what the problem might be. Same here.

Before posting you did read the sticky post at the top of the forum with the title Read Me: Read This Before Posting, didn't you?

No? Why not? Doesn't the title mean something?

anyone know why my calculated gross pay function isnt returning the right values? am i missing something in my parameter?

#include <iostream> 
#include<iomanip>
using namespace std; 
void getEmployeeData(int [ ], char [ ], int [ ], int);
void displayEmployeeInfo (int [ ], char [ ], float [ ], int);
float calculateGrossPay (char [ ], int [ ], int);

int main() 
{ 
int stoodunce[4]; //ep id
char stoodunce1[4];//payroll status
int stoodunce2[4];//input if its s/h/c
float stoodunce3[4];//calculates grosspay


getEmployeeData(stoodunce, stoodunce1 , stoodunce2,  4);
calculateGrossPay(stoodunce1, stoodunce2, 4);
displayEmployeeInfo(stoodunce, stoodunce1, stoodunce3, 4);




system("pause");
return 0; 
} 

void getEmployeeData(int id[ ], char ps[ ],int input[ ], int size)
{
for (int x = 0; x < size; x++)
{
cout << "Enter Employee ID : ";
cin >> id[x];
cout <<"Enter Payroll Status (s/h/c):";
cin >> ps[x];
if (ps[x] == 'S' || ps[x] == 's')
{
	cout <<"Enter Monthly Salary :";
	cin >> input[x];
}
else if (ps[x] == 'H' || ps[x] == 'h')
{
	cout <<"Enter total hours worked this month :";
	cin >> input[x];

}
else if (ps[x] == 'C' || ps[x] == 'c')
{
	cout <<"Enter total sales this month :";
	cin >> input[x];
}
}
}

float calculateGrossPay ( char ps [ ], int input [ ], int size)
{

float amount;

for (int x = 0; x < size; x++)
{

if (ps[x] == 'S' || ps[x] == 's')
		amount = input[x] + 0;

else if (ps[x] == 'H' || ps[x] == 'h')
		amount = 18.75 * input[x];

else
		amount = 1000 + input[x];
}
return amount;

}

void displayEmployeeInfo (int val[ ],char val1[ ], float val2 [ ], int size)
{
for (int x = 0; x < size; x++)
cout << "Employee ID:" << val[x] << " " << "Payroll Status" << " " << val1[x] << " " <<"GrossPay" << " " << val2[x] << endl;
}

anyone? i been working hard on this code =/

One, please format your code. Two, the code will be easier to follow if you pick more descriptive variable names:

float calculateGrossPay ( char ps [ ], int input [ ], int size)
{

float amount;

for (int x = 0; x < size; x++)
{

if (ps[x] == 'S' || ps[x] == 's')
		amount = input[x] + 0;

else if (ps[x] == 'H' || ps[x] == 'h')
		amount = 18.75 * input[x];

else
		amount = 1000 + input[x];
}
return amount;

}

Three, why bother with a loop? You are overwriting amount every trip through the loop, so why calculate the earlier iterations since they'll be overwritten? Perhaps you intend to have a running total and want to ADD to amount every trip through the loop rather than overwrite it (i.e. use += rather than = when adjusting the amount variable)?

yeah i think we have to running total for amount but ill do that later.

so i dont need float [ ] inside the bold section
float calculateGrossPay ( char ps [ ], int input [ ], int size)?

then do i also have to go back to the the top and also take off the "float [ ]" insde the parameter too?

if that made sense...

yeah i think we have to running total for amount but ill do that later.

so i dont need float [ ] inside the bold section
float calculateGrossPay ( char ps [ ], int input [ ], int size)?

then do i also have to go back to the the top and also take off the "float [ ]" insde the parameter too?

if that made sense...

Your array in the function should match your array in getEmployeeData . If that's an integer array there, make it an integer array here. If it's a float array there, make it a float array here. You are RETURNING a float or a double regardless due to this line:

amount = 18.75 * input[x];

amount cannot be an integer with the line above. Return the amount variable as you do. The return type of the function and the type of amount must match.

None of that has anything to do with my previous point though. My point was that if you are supposed to have a running total, that's not what you have now, which you say you're going to fix later.

i see.
well my calculategrosspay function matches with the array right?
how come its not outputing the right results for grosspay? it outputs -1.7374e +008? instead of a real number.

it looks like my paramaters match, am i leaving something off?

i see.
well my calculategrosspay function matches with the array right?
how come its not outputing the right results for grosspay? it outputs -1.7374e +008? instead of a real number.

it looks like my paramaters match, am i leaving something off?

I think you'd do well to comment and say explicitly what each function takes as parameters, what it returns, and what all the variables represent. In particular, what is intended to be an array and what isn't? It's not clear to me, at least, and the variable names are not descriptive, which makes it hard to debug. I suspect you have an array where you need to have a regular old float or int, or vice-versa, in at least one place, but I can't tell what your INTENT is with the function and the parameters in that regard. You also have this function call.

calculateGrossPay(stoodunce1, stoodunce2, 4);

This is a function that returns a float, but you lost that return value in the function call since you don't assign any variable to equal the return value. The net effect is that the line above has no effect on anything, so as you have it, you can delete that line and the program will be the same. You need to make use of the return value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.