Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

One last query about this program

Rodimus PrimeRodimus Prime Member Posts: 51
Hi,

I am almost 100% sure that the netpay should return the netpay, but it doesn't, it returns the grosspay.
Is there anything wrong in my code?

[code]
uses
crt;

const
normalhours = 40;
taxrate1 = 0.195;
taxrate2 = 0.33;
taxrate3 = 0.39;

var
empname : string[20];
payrate , errorcode : real;
workedhours , overtimehours , errorcode2 : real;
grosspay , netpay , taxdeduction : real;


function calgrosspay(workedhours, payrate: real): real;
var
paidhours, premiumhours : real;

begin
overtimehours := workedhours - normalhours;
if overtimehours <= 0 then
begin
paidhours := workedhours;
end
else if overtimehours > 0 then
begin
premiumhours := overtimehours * payrate * 1.5;
end;

paidhours := normalhours * payrate;
grosspay := premiumhours + paidhours;
calgrosspay := grosspay;
end;

function caltaxdeduction(grosspay: real): real;
begin
if grosspay <= 728.77 then
taxdeduction := grosspay * taxrate1
else
begin
taxdeduction := 728.77 * taxrate1;
if grosspay <= 1150.68 then
taxdeduction := taxdeduction + (grosspay - 728.77) * taxrate2
else
begin
taxdeduction := taxdeduction + (1150.68 - 728.77) * taxrate2;
taxdeduction := taxdeduction + (grosspay - 1150.68) * taxrate3;
end;
end;
caltaxdeduction := taxdeduction;
end;

begin
clrscr;

repeat
write('Enter the employees name: ');
readln(empname);

if(empname <> '') then
begin

repeat
write('Enter the hourly pay rate: ');
{$I-}
readln(payrate);
{$I+}
errorcode := IOResult;
if errorcode <> 0 then
begin
sound(1000);
delay(100);
nosound;
writeln;
writeln('Invalid value, enter a REAL number!');
writeln;
end;
until errorcode = 0;

repeat
write('Enter the hours worked this week: ');
{$I-}
readln(workedhours);
{$I+}
errorcode := IOResult;
if errorcode <> 0 then
begin
sound(1000);
delay(100);
nosound;
writeln;
write('Invalid value, enter a REAL number');
writeln;
end;
until errorcode = 0;
writeln;

netpay := calgrosspay(workedhours, payrate) - caltaxdeduction(grosspay);

writeln('Current employee is: ', empname);
writeln('Employee hourly pay rate is: $', payrate:2:2);
writeln('Employee gross pay for this weeks is: $', calgrosspay(workedhours, payrate):2:2);
writeln('Employee tax for this week is: $', caltaxdeduction(grosspay):2:2);
writeln('Employee net pay for this week is $', netpay:2:2);
writeln;
writeln;
writeln('Press ENTER if you wish to exit the program');
writeln;
end;
until empname = '';
end.

[/code]

Comments

  • Rodimus PrimeRodimus Prime Member Posts: 51
    : Hi,
    :
    : I am almost 100% sure that the netpay should return the netpay, but it doesn't, it returns the grosspay.
    : Is there anything wrong in my code?
    :
    : [code]
    : uses
    : crt;
    :
    : const
    : normalhours = 40;
    : taxrate1 = 0.195;
    : taxrate2 = 0.33;
    : taxrate3 = 0.39;
    :
    : var
    : empname : string[20];
    : payrate , errorcode : real;
    : workedhours , overtimehours , errorcode2 : real;
    : grosspay , netpay , taxdeduction : real;
    :
    :
    : function calgrosspay(workedhours, payrate: real): real;
    : var
    : paidhours, premiumhours : real;
    :
    : begin
    : overtimehours := workedhours - normalhours;
    : if overtimehours <= 0 then
    : begin
    : paidhours := workedhours;
    : end
    : else if overtimehours > 0 then
    : begin
    : premiumhours := overtimehours * payrate * 1.5;
    : end;
    :
    : paidhours := normalhours * payrate;
    : grosspay := premiumhours + paidhours;
    : calgrosspay := grosspay;
    : end;
    :
    : function caltaxdeduction(grosspay: real): real;
    : begin
    : if grosspay <= 728.77 then
    : taxdeduction := grosspay * taxrate1
    : else
    : begin
    : taxdeduction := 728.77 * taxrate1;
    : if grosspay <= 1150.68 then
    : taxdeduction := taxdeduction + (grosspay - 728.77) * taxrate2
    : else
    : begin
    : taxdeduction := taxdeduction + (1150.68 - 728.77) * taxrate2;
    : taxdeduction := taxdeduction + (grosspay - 1150.68) * taxrate3;
    : end;
    : end;
    : caltaxdeduction := taxdeduction;
    : end;
    :
    : begin
    : clrscr;
    :
    : repeat
    : write('Enter the employees name: ');
    : readln(empname);
    :
    : if(empname <> '') then
    : begin
    :
    : repeat
    : write('Enter the hourly pay rate: ');
    : {$I-}
    : readln(payrate);
    : {$I+}
    : errorcode := IOResult;
    : if errorcode <> 0 then
    : begin
    : sound(1000);
    : delay(100);
    : nosound;
    : writeln;
    : writeln('Invalid value, enter a REAL number!');
    : writeln;
    : end;
    : until errorcode = 0;
    :
    : repeat
    : write('Enter the hours worked this week: ');
    : {$I-}
    : readln(workedhours);
    : {$I+}
    : errorcode := IOResult;
    : if errorcode <> 0 then
    : begin
    : sound(1000);
    : delay(100);
    : nosound;
    : writeln;
    : write('Invalid value, enter a REAL number');
    : writeln;
    : end;
    : until errorcode = 0;
    : writeln;
    :
    : netpay := calgrosspay(workedhours, payrate) - caltaxdeduction(grosspay);
    :
    : writeln('Current employee is: ', empname);
    : writeln('Employee hourly pay rate is: $', payrate:2:2);
    : writeln('Employee gross pay for this weeks is: $', calgrosspay(workedhours, payrate):2:2);
    : writeln('Employee tax for this week is: $', caltaxdeduction(grosspay):2:2);
    : writeln('Employee net pay for this week is $', netpay:2:2);
    : writeln;
    : writeln;
    : writeln('Press ENTER if you wish to exit the program');
    : writeln;
    : end;
    : until empname = '';
    : end.
    :
    : [/code]
    :

    Please, could somebody confirm that there is nothing wrong, or is something wrong with my code.
    Coz if there isn't, then I'm sure that there is something wrong with my compiler.

  • anonedsanoneds Member Posts: 15
    : : Hi,
    : :
    : : I am almost 100% sure that the netpay should return the netpay, but it doesn't, it returns the grosspay.
    : : Is there anything wrong in my code?


    :
    : Please, could somebody confirm that there is nothing wrong, or is something wrong with my code.
    : Coz if there isn't, then I'm sure that there is something wrong with my compiler.
    :
    :

    I don't see anything wrong. I compiled it and it ran fine. Netpay wasn't equal to Grosspay. Can you give us the input you used?
    And all your output? And are you sure of your field formatting for the reals?

    Ben
  • Rodimus PrimeRodimus Prime Member Posts: 51
    [b][red]This message was edited by Rodimus Prime at 2004-3-28 3:19:15[/red][/b][hr]
    : : : Hi,
    : : :
    : : : I am almost 100% sure that the netpay should return the netpay, but it doesn't, it returns the grosspay.
    : : : Is there anything wrong in my code?
    :
    :
    : :
    : : Please, could somebody confirm that there is nothing wrong, or is something wrong with my code.
    : : Coz if there isn't, then I'm sure that there is something wrong with my compiler.
    : :
    : :
    :
    : I don't see anything wrong. I compiled it and it ran fine. Netpay wasn't equal to Grosspay. Can you give us the input you used?
    : And all your output? And are you sure of your field formatting for the reals?
    :
    : Ben
    :

    Hi,
    Thanks for your reply. Yeah, I guess it's something in my compiler then. But very strangely enough, when I made it into this

    [code]
    write(' Employee net pay for this week is $',
    calgrosspay(workedhours, payrate)-caltaxdeduction(grosspay):2:2);
    [/code]

    it did give me the correct figure for the netpay.

    Everything is rounded two to decimal places, and two significant figures, do you think this doesn't give me an accurate figure?

    EDIT - I have just discovered that if I enter in any worked hours as 40 or less, I get the really long real number as output.
    Can you please tell me how to fix that problem, and how I can avoid this output error in the future?

  • anonedsanoneds Member Posts: 15
    : [b][red]This message was edited by Rodimus Prime at 2004-3-28 3:19:15[/red][/b][hr]
    : : : : Hi,
    : : : :
    : : : : I am almost 100% sure that the netpay should return the netpay, but it doesn't, it returns the grosspay.
    : : : : Is there anything wrong in my code?
    : :
    : :
    : : :
    : : : Please, could somebody confirm that there is nothing wrong, or is something wrong with my code.
    : : : Coz if there isn't, then I'm sure that there is something wrong with my compiler.
    : : :
    : : :
    : :
    : : I don't see anything wrong. I compiled it and it ran fine. Netpay wasn't equal to Grosspay. Can you give us the input you used?
    : : And all your output? And are you sure of your field formatting for the reals?
    : :
    : : Ben
    : :
    :
    : Hi,
    : Thanks for your reply. Yeah, I guess it's something in my compiler then. But very strangely enough, when I made it into this
    :
    : [code]
    : write(' Employee net pay for this week is $',
    : calgrosspay(workedhours, payrate)-caltaxdeduction(grosspay):2:2);
    : [/code]
    :
    : it did give me the correct figure for the netpay.
    :
    : Everything is rounded two to decimal places, and two significant figures, do you think this doesn't give me an accurate figure?
    :
    : EDIT - I have just discovered that if I enter in any worked hours as 40 or less, I get the really long real number as output.
    : Can you please tell me how to fix that problem, and how I can avoid this output error in the future?


    Well, that doesn't make sense! Your "netpay := " statement contains the same as the write! And it works on my compiler...

    As for the output, I make the same mistake every time I try to print a real. I think "2:2" gives me two in front of the decimal point, and two behind. Even _this_ would be too small for many of your outputs. The "a:b" behind a real or variable means the following:

    a: the size of the entire field. This includes leading space, decimal point, and places after the decimal.

    b: the number of places after the decimal.

    If you change them to 8:2, you should have no problem.

    Ben
Sign In or Register to comment.