Hello.

I'm pretty new to the world of C++. I'm trying to write a program that shows the pay of different levels of employees:
managers -code 1- are on salary
hourlies -code 2- get str8time for 40 hours, then time & a half
commission -code 3- base of \$250 + 5.7% of sales
pieceworkers -code 4- fixed amount for every piece worked on

I'm stuck on how to designate the code 1, code 2, etc. for each classification.

Any help would be super!

-Clifton

## All 10 Replies

post some code to show you have attempted the problem if you want help. We dont handfeed you homework answers but will happily help you out if you show effort.

No problem, here is what I have so far:

``````//Clifton Copeland
//Excercise 5.21
//This program calculates the pay for a company's employees.

#include <iostream.h>

using namespace std:
using std::cout;
using std::cin;
using std::endl;

int main()
{

int payCode;
int payRate;
int oTRate;
int hours;
int grossPay;
int commPay;

cout << "Enter pay code: " << endl;
cin >> payCode;
if (payCode >= 5)
cout << "Enter a valid pay code in the range of 1-4. " << endl;
cout << "Enter pay code: " << endl;

//Pay code 1 statements.
if (payCode == 1)
cout << "Enter salary: " << endl;
cout << "Worker pay code: 1. " << endl;
cout << "Gross pay is " << grossPay << endl;
//End pay code 1 statements.

cout << "Enter pay code: " << endl;
cin >> payCode;
if (payCode >= 5)
cout << "Enter a valid pay code in the range of 1-4. " << endl;
cout << "Enter pay code: " << endl;

//Pay code 2 statements.
if (payCode == 2)
cout << "Enter the hourly pay rate: " << endl;
cin >> payRate;
cout >> "Enter the amount of hours worked: " << endl;
cin >> hours;

cout << "Worker pay code: 2. " << endl;
cout << "Hourly pay rate is: " << payRate << endl;
cout << "Hours worked: " << hours << endl;

oTRate = (hours - 40) * (payRate * 1.5);
grossPay = (payRate * hours) + oTRate * (hours - 40)

cout << "Gross pay is " << grossPay << endl;
//End pay code 2 statements.

cout << "Enter pay code: " << endl;
cin >> payCode;
if (payCode >= 5)
cout << "Enter a valid pay code in the range of 1-4. " << endl;
cout << "Enter pay code: " << endl;

return 0;
}``````

ok then.
firstly the correct header is <iostream> not <iostream.h>.

``````using std::cout;
using std::cin;
using std::endl;``````

can be removed. You have already said that you are using the whole of namespace std so these are superfluous.

Put your whole function inside a while(1) { // your code here } loop and add an option to quit to the menu. to do this just break out of the loop with a break command.

forget checking for errors at this stage, get it to work then we can show you how to deal with incorrect input.

``````if (payCode == 1)
cout << "Enter salary: " << endl;
cout << "Worker pay code: 1. " << endl;
cout << "Gross pay is " << grossPay << endl;``````

here you ask for salary but never get it from user.

``cout >> "Enter the amount of hours worked: " << endl;``

wrong operator!

``oTRate = (hours - 40) * (payRate * 1.5);``

is wrong. we know that otrate is payrate*1.5. hours have nothing to do with otrate.

``grossPay = (payRate * hours) + oTRate * (hours - 40)``

is also wrong. assuming otrate is now right otrate*(hours-40) is ok but payrate*hours is wrong if hours exceeds 40.

Think about the problem a little more, pick up on those points and repost your code. If its working we'll show you how to deal with bad input cleanly.

btw always use code tags to post code. its code and /code inside [] brackets.

I had changed around a lot of stuff before reading your reply. I made the changes you recommended except for: what sort of "while" statement should I use?

It compiled w/ no errors. Went to run it and everytime it outputs
"Enter pay code:"
It outputs
"Enter pay code:

This may have something to do with the while statement you had recommended. And did I do the right things using "double" instead of "int" for grossPay? I'm still not positive about when to use double to be honest.

``````//Clifton Copeland
//Excercise 5.21
//This program calculates the pay for a company's employees.

#include <iostream>

using namespace std;

int main()
{

int payCode;
int payRate;
int hours;
double grossPay;
int commPay;
int weeklySales;
int pieces;

cout << "Enter pay code: " << endl;
cin >> payCode;
if (payCode >= 5)
{
cout << "Enter a valid pay code in the range of 1-4. " << endl;
}
cout << "Enter pay code: " << endl;

//Pay code 1 statements.
if (payCode == 1)
{
cout << "Enter salary: " << endl;
cin >> payRate;
cout << "Worker pay code: 1. " << endl;
cout << "Gross pay is " << grossPay << endl;
}
//End pay code 1 statements.

cout << "Enter pay code: " << endl;
cin >> payCode;
if (payCode >= 5)
{
cout << "Enter a valid pay code in the range of 1-4. " << endl;
}
cout << "Enter pay code: " << endl;

//Pay code 2 statements.
if (payCode == 2)
{
cout << "Enter the hourly pay rate: " << endl;
cin >> payRate;
cout << "Enter the amount of hours worked: " << endl;
cin >> hours;

cout << "Worker pay code: 2. " << endl;
cout << "Hourly pay rate is: " << payRate << endl;
cout << "Hours worked: " << hours << endl;

if (hours > 40)
{
grossPay = ((hours -40) * payRate * 1.5) + payRate * 40;
}
if (hours <= 40)
{
grossPay = hours * payRate;
}
cout << "Gross pay is " << grossPay << endl;
}
//End pay code 2 statements.

cout << "Enter pay code: " << endl;
cin >> payCode;
if (payCode >= 5)
cout << "Enter a valid pay code in the range of 1-4. " << endl;
cout << "Enter pay code: " << endl;

//Pay code 3 statements.
if (payCode == 3)
{
cout << "Enter the gross sales amount: " << endl;
cin >> weeklySales;

cout << "Worker pay code: 3. " << endl;
cout << "Base pay rate is 250. " << endl;
cout << "Weekly sales: " << weeklySales << endl;
cout << "Commission rate is 5.7% " <<  endl;

grossPay = 250 + (weeklySales * .057);

cout << "Gross pay is " << grossPay << endl;
}
//End pay code 3 statements.

cout << "Enter pay code: " << endl;
cin >> payCode;
if (payCode >= 5)
{
cout << "Enter a valid pay code in the range of 1-4. " << endl;
}
cout << "Enter pay code: " << endl;

//Pay code 4 statements.
if (payCode == 4)
{
cout << "Enter pay rate per piece: " << endl;
cin >> payRate;
cout << "Enter the amount of pieces completed: " << endl;
cin >> pieces;

cout << "Worker pay code: 4. " << endl;
cout << "Pay rate per piece: " << payRate << endl;
cout << "Pieces completed this week: " << pieces << endl;

grossPay = payRate * pieces;

cout << "Gross pay is " << grossPay << endl;
}

return 0;
}``````

Thank you for the help!

what I meant is illustrated below. it breaks my heart to write all of this in the main function but it seems as if you havent learnt functions yet so I tried not to get too far ahead. I have shown you how to validate and deal with incorrect input on the menu entry. When you need input in the bits of this you write you can use the same technique.
anyway....

``````#include <iostream>
#include <limits>

using namespace std;

int main()
{
while(1) // an infinite loop
{
cout<<endl;
cout<<"1 :- Managers"<<endl;
cout<<"2 :- Hourly workers"<<endl;
cout<<"3 :- Commission workers"<<endl;
cout<<"4 :- Piece workers"<<endl;
cout<<"5 :- Exit"<<endl;
cout<<"Enter code :- ";
while(!(cin >> menucode) || menucode < 1 || menucode > 5) // get an int between 1 and 5
{
cout<<endl<<"INCORRECT CHOICE. REENTER NOW :-";
cin.clear(); // this clears the error flags
cin.ignore(numeric_limits<streamsize>::max(),'\n'); // this clears the junk input from cin
}
break; // this drops us out of the loop to the return statement below it
{
// deal with salaried workers
}
{
// deal with hourly workers
}
{
// deal with commission workers
}
{
// deal with piece workers
}
}

return 0;
}``````
``#include  <limits>``

When I put this in, it is now giving me an error in line 7 and says "limits: no such file or directory".

I've not dealt with the <limits> tag yet, so I don't really know what's going on with it.

-Clifton

What compiler are you using?
You will need something reasonably standards conforming. You can get one in my sig.

Actually, Dev C++ is what I'm currently using.

Actually, Dev C++ is what I'm currently using.

What version? Older versions came with an older, non-conformant compiler. Get the newest version.

I'm using version 4. The only thing newer are all betas. Are any of them stable enough to be reliable?

Also, I'm supposed to be using a switch in the coding above. I'm really totally unsure how to go about that.

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.