I have a program to write and im stuck. It runs but not properly. idk i think my "if" "else" statements are the problem.

here are some sample runs

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

Employee ID: 123
Payroll Status: Salaried
Gross Pay: $5280


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

Employee ID: 111
Payroll Status: Hourly
Gross Pay: $1509.38


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

Employee ID: 999
Payroll Status: Commissioned
Gross Pay: $1945

for ex. when I enter H for hourly worker it asks for hours worked then it asks me for "total sales this month" and thats supposed to be ask if i enter C for comissioned worker -__-

#include <iostream>
using namespace std;

void getEmployeeData (int*, char*, int*);
float calculateGrossPay(char, 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 (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)
{
	cout <<"Enter Employee ID :";
	cin >> *id;

	cout <<"Enter Payroll Status :";
	cin >> *type;
	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 (*type == 'C' || *type == 'c');
		{
		cout <<"Enter total sales for month :";
		cin >> *amount;
		}
}

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

	if (c == 'A' || c == 'a')
		f = a + 0;

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

	else 
		f = 1000 + a;

	return f;
}

Recommended Answers

All 3 Replies

Member Avatar for r.stiltskin

remove the semicolon from the end of this line

else (*type == 'C' || *type == 'c');

hmm i did and now it doesnt compile? 2 errors.

It's because you are doing another comparison. What you need is to add an if before the () like

else if (*type == 'C' || *type == 'c')

That should take care of the issue and avoid a few more that could have resulted. Make sure you do that to both the line in getEmployeeData and main.


Also, is your calculateGrossPay function correct? You are passing a character that should be either an S, s, H, h, c, or C, and yet you tell it to look for an A, a, h, or H, and if none of those (meaning all other letters), just do the last statement. Is that really what that function is suppose to be doing?

I believe the (A or a) is suppose to be (S or s). And, the second part isn't really a big deal. Having the "if, else if, else" is fine since your program doesn't output anything if the letters are incorrect, but I just wanted to point that out anyway cause I've seen people flip out over why their code isn't working and technically it's doing exactly what they told it to do.

One thing you might want to look into is a switch statement. It handles all the comparisons for you. Another thing is instead of writing all cases, change all the letters to either upper case or lower case, and just deal with that one case. Both can be accomplished just by using either toupper() or tolower().

Just to show you what it would do, this is what your one function would look like with a switch and changing all letters to upper case.

void getEmployeeData (int* id, char* type, int* amount)
{
	cout <<"Enter Employee ID :";
	cin >> *id;

	cout <<"Enter Payroll Status :";
	cin >> *type;
	switch (toupper(*type))
	       { case 'S':
		      cout <<"Enter monthly salary :";
		      cin >> *amount;
		      break;
                 case 'H':
                      cout <<"Enter number of hours worked this month :";
		      cin >> *amount;
		      break;
		 case 'C':
                      cout <<"Enter total sales for month :";
		      cin >> *amount;
		      break;
                 default:
                      cout << "ERROR: Invalid type..." << endl;
                      break;
           }
    cout << endl;
}

The default option in the switch is there to catch all the other values that aren't S, s, h, H, C, or c so that they user (or programmer) will know that something went wrong without needing to analyze the input.

It's helpful cause you could run a do while loop and add a check variable, then, if they don't enter one of the above letters, the check variable is set to false, say "invalid input" and it loops back up to the top to ask again. If it's good input, set the variable to true and it won't loop. It's quite sneaky but helpful if you are prone to wanting the letter 'y' and end up with 't' all the time. Plus, with your program, just adding the loop to this one function would ensure that all the other functions work correctly later. Right now, if someone types in anything but those three letters, the program doesn't output anything else but the "Press any key to continue . . ." line.

Hope this helps.

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.