Click to See Complete Forum and Search --> : help please
yawningdog
01-28-2002, 06:43 PM
Here is the code I've written
#include<iostream.h>
float payCalc(int labor, float sub, int percent);
int main()
{
int labor, percent;
float pay, sub;
char choice='y';
while (choice=='y')
{
cout<< "Enter your labor for the week.\n";
cin>> labor;
if (labor<=250)
percent=10, sub=0;
if (labor>250 && labor<=500)
percent=15, sub=25;
if (labor>500 && labor<=750)
percent=20, sub=62.5;
if (labor>750 && labor<=1000)
percent=25, sub=112.5;
if (labor>1000 && labor<=1550)
percent=30, sub=175;
if (labor>1550)
percent=35, sub=367.5;
pay=payCalc(labor, sub, percent);
cout<< "\nYour commission pay should be "<<pay<<" dollars.";
cout<< "\nCalculate another amount? (y/n)\n";
cin>> choice;
cout<< "\n";
}
return 0;
}
float payCalc(int labor, float sub, int percent)
{
float remaind;
if (labor<=1550)
{
if (labor<=1000)
remaind=labor%250;
else
remaind=labor%1000;
}
else
remaind=labor%1550;
return (((remaind*percent)/100)+sub);
}
It's a program that calculates commission pay where I work. If Explanation is needed, I can provide.
Anyway, I want payCalc to return a float to two decimal places (as dealing with money of course) but I can't get it to return an accurate one. I think I need to declare labor as a float, but I get errors that say in payCalc I cannot use a float as an operand for the modulus operator.
Secondly, I'd like for someone to grade this for me. What have I done right or wrong, aside from the problem stated.
please wrap your code in UBB code tags (the word code in square brackets)
[.code]
your code here
[/code]
without the period before 'code'
That way we can read your code, and maybe be more helpful.
:)
yawningdog
01-28-2002, 11:04 PM
Sorry. How's this?
#include<iostream.h>
float payCalc(int labor, float sub, int percent);
int main()
{
int labor, percent;
float pay, sub;
char choice='y';
while (choice=='y')
{
cout<< "Enter your labor for the week.\n";
cin>> labor;
if (labor<=250)
percent=10, sub=0;
if (labor>250 && labor<=500)
percent=15, sub=25;
if (labor>500 && labor<=750)
percent=20, sub=62.5;
if (labor>750 && labor<=1000)
percent=25, sub=112.5;
if (labor>1000 && labor<=1550)
percent=30, sub=175;
if (labor>1550)
percent=35, sub=367.5;
pay=payCalc(labor, sub, percent);
cout<< "\nYour commission pay should be "<<pay<<" dollars.";
cout<< "\nCalculate another amount? (y/n)\n";
cin>> choice;
cout<< "\n";
}
return 0;
}
float payCalc(int labor, float sub, int percent)
{
float remaind;
if (labor<=1550)
{
if (labor<=1000)
remaind=labor%250;
else
remaind=labor%1000;
}
else
remaind=labor%1550;
return (((remaind*percent)/100)+sub);
}
Okay, I can answer one of your questions. -(and I could be wrong on this)- A float is a float is a float. There's no such thing as a 'float to two decimal places'. Also note, a float is stored in memory in a rather strange (to humans) representation, in the interests of space, which ends up being not always being completely accurate. (So, for example, 33/6 I believe exactly how a float is stored is a characteristic of your computer, not a programming language or program. The best thing I know of to do is keep the float as it is and only worry about how many decimal places you see when you print it out. (printf("%.2f", pay)) is the C version, I know cin and cout have ways to format data, too.
I didn't notice any obvious problems with your code. :)
Stuka
01-29-2002, 10:47 AM
return (((remaind*percent)/100)+sub);
<HR></blockquote>
Your problem is in here - since percent and 100 are both ints, remaind is getting truncated to an int. Try this:
return (((remaind*(float)percent/(float)100)+sub);
<edit>Missed a )</edit>
[ 29 January 2002: Message edited by: Stuka ]
Strike
01-29-2002, 11:34 AM
Well, the "round off to the nearest cent" business could easily be solved by simply storing the number of cents, and then simply reformatting the output when displaying it or using temporary floats and dividing by 100 to display dollars and cents.
yawningdog
01-29-2002, 10:28 PM
Okay, I've obviously failed to explain the problem correctly. Allow me to try again.
The program works perfectly as long as I enter a whole number for labor. If labor = 2312 then the program prints out a value of 634.2 which is correct (I think). If I enter 2312.43 I get 634.2 again. I know this is a float and it should not work since I've declared labor as an int, but if I declare labor as a float, It wont compile because apparently it cannot be used with the modulus operator.
That's the goal. I want to declare labor as a float, but I can't get the program to compile as such.
Additionally, If I enter a float for labor which I know I cannot legally do, the loop doesn't work as designed. The pay figure is output, the choice is asked for, and then the program simply shuts down as if something other than "y" has been already entered for "choice".
The question of rounding off to the nearest hundredth for "pay" was something I still needed to discuss, but I was going to address that later.
Thank you all so much for your patience.
Stuka
01-30-2002, 02:59 PM
I see now. You'll have to write a modulus function, as the % operator will not work for you here. Try this:float modulus(float dividend, float divisor){
return (dividend - (divisor * floor(dividend / divisor)));
}
Replace your x%y with modulus(x,y), and call labor a float. Actually, this SHOULD be done with a template, but I'm feeling lazy. Oh, and check the floor() call - I know it exists, but the name may be different. The problem with entering a float into an int is simple - NEVER TRUST USER INPUT. Take in a string, and convert it to your float/int/whatever - and make sure the input is valid. You're experiencing a classic buffer overflow.
bwkaz
01-30-2002, 03:28 PM
The reason it exits right after it prints stuff out is that cin >> labor will stop accepting characters from the stream after it sees one that isn't a digit. So when it sees the decimal point in your input, it leaves the rest of the numbers out there, waiting for the next >> call. Then you do a cin >> choice, and so choice gets the value '3' (assuming you entered 54342.36, or something similar).
You did enter something other than y, according to the iostream library.
Like Stuka said, NEVER TRUST USER INPUT. Grab it all at once, into a character array or string (preferably a string), and parse it yourself.
yawningdog
01-30-2002, 06:25 PM
Okay, I kind of get it and I kind of don't. I am still very new at this.
Stuka- am I going to have to declare this new function as I did the other one? Will I be calling this function from within payCalc, or writing it as part of payCalc? I'm afraid the floor() call is as yet greek to me, so I'll have to skip forward in the book to figure out what it is/does, unless you or someone else wants to take a whack at explaining it. For future reference, is the % operand only to be used with int variables?
bwkaz- You've vindicated me as the problem with the loop is as I thought it might be. Thanks.
Never trust user input sounds like the best advice I've heard yet.
[ 30 January 2002: Message edited by: yawningdog ]
TheLinuxDuck
01-30-2002, 06:47 PM
Originally posted by yawningdog:
<STRONG>Never trust user input sounds like the best advice I've heard yet.</STRONG>
And just in case it hasn't fully sunk in.... never trust user input!
(^=
I am not saying this to be rude or try to crack a joke.. I am seriously telling you that as Stuka and bwkaz have said, never expect user input to be what you think it should be. There are alot of trick and techniques for dealing with this sort of thing, but what I know applies more to C than c++, so I can't help you much.. (^=
Your programs will be happier if you write with this in mind from the beginning.
bwkaz
01-30-2002, 07:48 PM
Yes, the % operator is for ints only. Not sure why, but that's the way it is.
You will want to basically copy and paste Stuka's code to a place in your .cpp (or whatever it is) file before where you call it. It's just another function like payCalc.
Then do as Stuka said and replace every occurance of (labor % <whatever> ) in payCalc with the function call modulus(labor, <whatever> ).
floor is a predefined math library function that takes a float and returns the largest integer that is less than the inputted float. But you have to #include <math.h> at the beginning of your source file to use it, and on Linux at least, you have to link with the math library (g++ -o paycalc paycalc.cpp -lm would be close to the command line).
HTH
[ 30 January 2002: Message edited by: bwkaz ]
yawningdog
01-30-2002, 08:23 PM
Fantastic. It's working now. One final question.
How do I get an answer rounded to the nearest hundredth, as previously discussed?
I still don't quite understand floor(), but after only about 2 months of writing code, I think I can forgive myself just this once.
You folks are the best :D
Stuka
01-31-2002, 01:06 PM
To set the precision (number of digits output) for cout:cout << setprecision(2) << myfloat;
The floor function is essentially a "round-down" function - it returns the highest integer value less than or equal to the input value. For example, floor(5.2) = 5.0, while floor(4.0) = 4.0, and floor(8.999) = 8.0.
yawningdog
02-01-2002, 06:43 PM
Okay, having trouble with the setprecision() function. Do I have to include another library or do I write it myself?
Stuka
02-01-2002, 07:02 PM
sorry...you need to:#include <iomanip>
yawningdog
02-02-2002, 10:20 AM
I really hate to be a fusspot, but now my answer is coming out in scientific notation.