|
-
June 29th, 2009, 11:12 PM
#1
1 small issue and im happy!!! =)
Ok all, new to programming, but feeling better about it. ive written this program for my class and i feel very good about it. however, there is one issue im having trouble making right. if you run the program you will see what im talking about, but essentially here it is...
this is a simple program that calculates 4 differant pay rates based on the type of employee that is entered within the switch statement. that said, part of it is me telling the program to figure the pay rate on a 40 hr work week plus overtime. the problem is this. i can get the program to kick back if someone enters more than 40 hours on a work week, and i can get it to kick back if the user enters says -3 hours. However if say they enter 45 hrs, but then enter -3 right after in the next line, it accepts it. the same if first they enter -7 hrs, but then enter 1000 it accepts that. for some reason the statements are not connecting to one another and thats where my issue is. how do i make it recognize both, instead of only 1 or the other. Listed below is my program and any help is truly appreciated.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float reghours=0, overtime=0, overtimepay, grossweeklysales, commisionpay, commision, pieces, grosspay, hourlywage=0, piecepay;
int n;
n = 1,2,3,4;
enum Etype {Manager, Hourly, Commision, Piece};
cout<<" Please enter the approprate employee code to be figure. Enter 1 for Managers, 2 for Hourly workers, 3 for Commissioned workers, and 4 for Piece workers: ";
cin>> n;
switch (n) {
case 1:
cout<< "The Manager will receive 1000 dollars for the week.";
break;
case 2:
cout<< "Please enter the number of hours worked between 1 and 40: ";
cin>>reghours;
do {
if (reghours >=41) {
cout<< "Hours must be between 1 and 40, please reenter hours worked: ";
cin>>reghours;
}
} while (reghours >=41);
do {
if (reghours <=0) {
cout<< "Hours must be between 1 and 40. Please reenter hours worked: ";
cin>>reghours;
}
} while (reghours <=0);
cout<< "Please enter the number of hours worked over 40: ";
cin>>overtime;
do {
if (overtime >=81) {
cout<< "Overtime hours cannot be greater than 80. Please reenter the number of overtime hours worked: ";
cin>>overtime;
}
} while (overtime >=81);
cout<< "Please enter the hourly rate pay to this employee: ";
cin>>hourlywage;
do {
if (hourlywage >=251) {
cout<< "No employees within this company are paid more than 250 dollars per hour. Please reenter the hourly rate for this employee: ";
cin>>hourlywage;
}
} while (hourlywage >=251);
do {
if (hourlywage <=0) {
cout<< "Hourly wage must be a minimum of 1 dollar per hour. Pleaser reenter the hourly rate for this employee: ";
cin>>hourlywage;
}
} while (hourlywage <=0);
overtimepay = overtime * (1.5 * hourlywage);
grosspay = (reghours * hourlywage) + overtimepay;
cout<< "This employees pay for the week is: " << grosspay << endl;
break;
case 3:
cout<< "Please enter the amount of gross weekly sales this employee contibuted to: ";
cin>>grossweeklysales;
commisionpay = (grossweeklysales / 5.7) + 250;
cout<< "This emloyees pay for the week is: " << commisionpay <<endl;
break;
case 4:
cout<< "please enter the number of peices this employee completed: ";
cin>>pieces;
do {
if (pieces >= 100); {
cout<< "Employees are not allowed to produce more than 100 units per week. Please reenter the number of units produced this week: ";
cin>>pieces;
}
} while (pieces >=101);
do {
if (pieces <0); {
cout<< " Employees cannot produce a negative number of pieces. Please reenter the number of pieces produced: ";
cin>>pieces;
}
} while (pieces <0);
piecepay = pieces * 50;
cout<< "This employees pay for the week is: " << piecepay <<endl;
break;
}
}
-
June 30th, 2009, 12:00 AM
#2
Re: 1 small issue and im happy!!! =)
 Originally Posted by [email protected]
[code]
switch (n) {
case 1:
cout<< "The Manager will receive 1000 dollars for the week.";
break;
case 2:
cout<< "Please enter the number of hours worked between 1 and 40: ";
cin>>reghours;
do {
if (reghours >=41)
{
cout<< "Hours must be between 1 and 40, please reenter hours worked: ;
cin>>reghours;
}
} while (reghours >=41);
Here in the above code... If the user enters.... -3( which is not >=41) will be acccepted.
Code:
do {
if (reghours <=0) {
cout<< "Hours must be between 1 and 40. Please reenter hours worked: ";
cin>>reghours;
}
} while (reghours <=0);
and when this loop comes since -3 is < 0 it will ask the user to re-enter the no.... and if the user enters 1000 it will accept that... because it is not (1000 ! <=0) that is why your code is behaving the way you mentioned.... This will happen for your other ahead loops also...
Better put the conditional checks in correct way... as i have metioned below...
Code:
case 2:
cout<< "Please enter the number of hours worked between 1 and 40: ";
cin>>reghours;
while (reghours >=41 || reghours <= 0)
{
cout<< "Hours must be between 1 and 40, please reenter hours worked: ;
cin>>reghours;
}
Also you can remove those if conditions using the above method...
Do the same for you other validations... Note: use "code" and "/code" tags to post your code snippet.
Hope this helps.... 
Thanks,
Last edited by LOOSER_007; June 30th, 2009 at 12:06 AM.
-
July 6th, 2009, 04:52 PM
#3
Re: 1 small issue and im happy!!! =)
well, that fixed it all. i tried the or statement before and it didnt like it, but this time.... it did! =) i got an A on my project by the way, so thanks for the assistance.
now i have to write a blackjack program. something i feel will be quite a bit tougher. learning array variables now to figure out how to make the deck of cards. although im still not sure how that will work. but eh, ive got 2 weeks.
Thanks again.
Blue
-
July 6th, 2009, 05:57 PM
#4
Re: 1 small issue and im happy!!! =)
Blackjack programs are fun. I wrote one in TrueBASIC years ago, drawing the cards visually using nothing but circles and polygons----some people actually asked me if I'd imported Solitaire's graphics on that one.....(Of course, a basic program doesn't require graphics at all.)
-
July 6th, 2009, 06:08 PM
#5
Re: 1 small issue and im happy!!! =)
ya i think it will be enjoyable. again, im still learning a lot of what i will need to know in order to write the program, but im getting there. the array function seems like it will def be useful for the deck of cards, and then i just learned about the srand function as opposed to the basic rand function. so we will see what happens next. on a short class break right now.
Anyway, thanks again to all who helped out. ive tried some other forums (cough cplusplus cough) and got some very rude responses. people seem much more willing to give solid advice and help here.
Blues
-
July 6th, 2009, 07:45 PM
#6
Re: 1 small issue and im happy!!! =)
In the future you should use code tags around any nontrivial amounts of code....you're more likely to get good responses if folks have proper indenting to work with.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|