turbo c calculations not right - Programmers Heaven

Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

turbo c calculations not right

michelgomoez29michelgomoez29 Posts: 1Member
i don't know whats wrong, but it wont show the correct answers or the computation. though it has no errors
this is my source code

#include
#include

float workhrs, overtime, salary, tax, tsalary;
long int rate;
char name[80],choose;



try()
{
clrscr();
textcolor (3);
cprintf("
How many hours have you been working? ");
scanf("%f",&workhrs);


if(workhrs>=100 && workhrs<=25)
{
printf("
WORK HOUR is not VALID!");
getch();
try();
}
else
{
textcolor (3);
printf("
Enter your hourly rate: ");
scanf("%d",&rate);
textcolor (3);
cprintf("


Press any key to compute salary...");
getch();
if(workhrs>40)
{
if(workhrs>=40 && workhrs<=50)
{
overtime=workhrs-40;
salary=(40*rate)+(overtime*40);
}
else
{
overtime=workhrs-40;
salary=(rate*40)+(overtime*40)+200;
}
}
else
{
salary=workhrs*rate;
}




clrscr();



if(salary>8000)
{
tax=salary*.15;
tsalary=salary-tax;

textcolor (15);
cprintf("
Hello %s! Your salary for this month is P%.2f", name, salary);
getch();
clrscr();
gotoxy(1,15);
textcolor (15);
cprintf("
There is a 15 percent tax deduction which is P%.2f",tax);
getch();
clrscr();
gotoxy(1,15);
printf("
Your total salary with 15 percent tax deduction is P%.2f",tsalary);
getch();
clrscr();

}

else
{
printf("
Hello %s %s! Your salary for this month is P%.2f", name, salary);
getch();
}

}
}


main()
{
clrscr();
textcolor (3);
printf("
Enter name: ");
gets(name);
try();
clrscr();
gotoxy(30,45);


printf("
Would you like to try again? Y/N

");
scanf("%s",&choose);
if (choose=='y')
main();
else
{gotoxy(33,11);
textcolor (15);
cprintf("HAVE A NICE DAY!");
getch();}
;

}

Comments

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [color=Blue]Turbo C has a very easy to use debugger - I am sure if you use it - you will see what is wrong. Set a breakpoint AFTER any input, like that:[/color]
    [code]
    scanf ("%f", &value);
    <-- set bkpt on this line and chk 'value'
    [/code]
    [color=Blue]When program stops in debugger on the next line inspect the variable 'value' and verify that whatever you entered is realy there. Do the same AFTER calculations - verify that all calculations are sound. TC debugger has WATCH window and INSPECT window - see the menu bar items - you will find how to open them. WATCH allows to see how variable changes on every line of the program - you basically watch the variable and continue step line-by-line. INSPECT - once open - must be closed to continue stepping. To put a variable into one of these windows - set a cursor on the variable and use menu item to open appropriate panel.[/color]
  • HK_MP5KPDWHK_MP5KPDW Posts: 770Member ✭✭✭
    Please use code tags when posting your code samples. Here is your program reposted with code tags provided (and formatted).
    [code]
    #include
    #include

    float workhrs, overtime, salary, tax, tsalary;
    long int rate;
    char name[80],choose;



    try()
    {
    clrscr();
    textcolor (3);
    cprintf("
    How many hours have you been working? ");
    scanf("%f",&workhrs);


    if(workhrs>=100 && workhrs<=25)
    {
    printf("
    WORK HOUR is not VALID!");
    getch();
    try();
    }
    else
    {
    textcolor (3);
    printf("
    Enter your hourly rate: ");
    scanf("%d",&rate);
    textcolor (3);
    cprintf("


    Press any key to compute salary...");
    getch();
    if(workhrs>40)
    {
    if(workhrs>=40 && workhrs<=50)
    {
    overtime=workhrs-40;
    salary=(40*rate)+(overtime*40);
    }
    else
    {
    overtime=workhrs-40;
    salary=(rate*40)+(overtime*40)+200;
    }
    }
    else
    {
    salary=workhrs*rate;
    }

    clrscr();

    if(salary>8000)
    {
    tax=salary*.15;
    tsalary=salary-tax;

    textcolor (15);
    cprintf("
    Hello %s! Your salary for this month is P%.2f", name, salary);
    getch();
    clrscr();
    gotoxy(1,15);
    textcolor (15);
    cprintf("
    There is a 15 percent tax deduction which is P%.2f",tax);
    getch();
    clrscr();
    gotoxy(1,15);
    printf("
    Your total salary with 15 percent tax deduction is P%.2f",tsalary);
    getch();
    clrscr();
    }
    else
    {
    printf("
    Hello %s %s! Your salary for this month is P%.2f", name, salary);
    getch();
    }

    }
    }


    main()
    {
    clrscr();
    textcolor (3);
    printf("
    Enter name: ");
    gets(name);
    try();
    clrscr();
    gotoxy(30,45);


    printf("
    Would you like to try again? Y/N

    ");
    scanf("%s",&choose);
    if (choose=='y')
    main();
    else
    {
    gotoxy(33,11);
    textcolor (15);
    cprintf("HAVE A NICE DAY!");
    getch();
    }

    }[/code]

    Some comments:

    1) You are using recursion in a manner I believe to be inappropriate. You should be using loops instead of recursion when a user enters an invalid amount of work hours or when the user wishes to repeat the program. In particular, you should never recursively call [italic]main[/italic].

    2) Don't use [italic]gets[/italic]. It is dangerous as it does not prevent the user from entering more characters than is possible for the array to hold. You should instead prefer [italic]fgets[/italic] which allows you to specify the maximum number of characters your buffer can hold.

    3) Avoid compiler/OS specific functions wherever possible.

    4) Upgrade yourself to a better/newer compiler. Turbo C was around back when most of mankind still lived in caves. There are a number of free ones out there that have been created this millennium.

    5)
    [code]if(workhrs>40)
    {
    if(workhrs>=40 && workhrs<=50)
    {
    overtime=workhrs-40;
    salary=(40*rate)+([color=Red]overtime*40[/color]);
    }
    else
    {
    overtime=workhrs-40;
    salary=(rate*40)+([color=Red]overtime*40[/color])+200;
    }
    }[/code]
    Shouldn't overtime be calculated as [b]overtime*rate*1.5[/b] (for time-and-a-half).

    6)
    [code]else
    {
    printf("
    Hello [color=Red][b]%s %s[/b][/color]! Your salary for this month is P[color=Red][b]%.2f[/b][/color]", [color=Blue][b]name[/b][/color], [color=Blue][b]salary[/b][/color]);
    getch();
    }
    [/code]
    Pay attention to how many format specifiers you are using and how many additional arguments you are passing to [italic]printf[/italic].
  • Elroy015Elroy015 Posts: 18Member
    This post has been deleted.
  • Elroy015Elroy015 Posts: 18Member
    This post has been deleted.
Sign In or Register to comment.