C and C++

Moderators: None (Apply to moderate this forum)
Number of posts: 94715

This Forum Only

turbo c calculations not right Posted by michelgomoez29 on 21 Mar 2011 at 5:53 PM
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<stdio.h>
#include<conio.h>

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

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

if(workhrs>=100 && workhrs<=25)
{
printf("\nWORK HOUR is not VALID!");
getch();
try();
}
else
{
textcolor (3);
printf("\n Enter your hourly rate: ");
scanf("%d",&rate);
textcolor (3);
cprintf("\n\n\n 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("\n Hello %s! Your salary for this month is P%.2f", name, salary);
getch();
clrscr();
gotoxy(1,15);
textcolor (15);
cprintf("\n There is a 15 percent tax deduction which is P%.2f",tax);
getch();
clrscr();
gotoxy(1,15);
printf("\n Your total salary with 15 percent tax deduction is P%.2f",tsalary);
getch();
clrscr();

}

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

}
}

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

printf("\nWould you like to try again? Y/N\n\n");
scanf("%s",&choose);
if (choose=='y')
main();
else
{gotoxy(33,11);
textcolor (15);
cprintf("HAVE A NICE DAY!");
getch();}
;

}
Re: turbo c calculations not right Posted by AsmGuru62 on 22 Mar 2011 at 3:59 AM
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:
```scanf ("%f", &value);
<next line>           <-- set bkpt on this line and chk 'value'
```

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.
Re: turbo c calculations not right Posted by HK_MP5KPDW on 22 Mar 2011 at 12:13 PM
Please use code tags when posting your code samples. Here is your program reposted with code tags provided (and formatted).
```#include<stdio.h>
#include<conio.h>

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

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

if(workhrs>=100 && workhrs<=25)
{
printf("\nWORK HOUR is not VALID!");
getch();
try();
}
else
{
textcolor (3);
printf("\n Enter your hourly rate: ");
scanf("%d",&rate);
textcolor (3);
cprintf("\n\n\n 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("\n Hello %s! Your salary for this month is P%.2f", name, salary);
getch();
clrscr();
gotoxy(1,15);
textcolor (15);
cprintf("\n There is a 15 percent tax deduction which is P%.2f",tax);
getch();
clrscr();
gotoxy(1,15);
printf("\n Your total salary with 15 percent tax deduction is P%.2f",tsalary);
getch();
clrscr();
}
else
{
printf("\n Hello %s %s! Your salary for this month is P%.2f", name, salary);
getch();
}

}
}

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

printf("\nWould you like to try again? Y/N\n\n");
scanf("%s",&choose);
if (choose=='y')
main();
else
{
gotoxy(33,11);
textcolor (15);
cprintf("HAVE A NICE DAY!");
getch();
}

}```

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 main.

2) Don't use gets. 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 fgets 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)
```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;
}
}```

Shouldn't overtime be calculated as overtime*rate*1.5 (for time-and-a-half).

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

Pay attention to how many format specifiers you are using and how many additional arguments you are passing to printf.
This post has been deleted. Posted by Elroy015 on 1 Apr 2011 at 8:20 PM
This post has been deleted.
This post has been deleted. Posted by Elroy015 on 1 Apr 2011 at 8:22 PM
This post has been deleted.

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic