C/introduction to C programing
Expert: Narendra - 10/9/2005
Question-------------------------
Followup To
Question -
Hi,
This is what I did
#include<stdio.h>
#include<conio.h>
int main (void){
int EN;
float GP,DA,BA,NP,H,PR,Salary;
char MS,Single,Married,PID,S,I,J,M;
printf("please enter the employee number");
scanf("%d",&EN);
printf("please enter the employee worked");
scanf("%f",&H);
printf("please enter the employee Position ID");
scanf("%c",&PID);
printf ("please enter the employee marital status");
scanf("%c"&MS);
if (PID==S&&MS==Single){
GP=H*18.00;
BA=DA-10.00;}
else if(PID==S&&MS==Married){
GP=H*18.00;
BA=DA-20.00;}
this is an example of a senior employee followup the same for the intermediate,junior and manager.Only for manager GP=Salary
When I compile it there are no errors but when I run it doesnot work thankyou for your help.
Answer -
Without indentation it is very difficult to read and understand a program.
Here is your code with indentation:
#include<stdio.h>
int main ()
{
int EN;
float GP,DA,BA,NP,H,PR,Salary;
char MS,Single,Married,PID,S,I,J,M;
printf("please enter the employee number");
scanf("%d",&EN);
printf("please enter the employee worked");
scanf("%f",&H);
printf("please enter the employee Position ID");
scanf("%c",&PID);
printf ("please enter the employee marital status");
scanf("%c", &MS);
if ((PID == S) && (MS == Single))
{
GP = H * 18.00;
BA = DA - 10.00;
}
else if ((PID == S) && (MS == Married))
{
GP = H * 18.00;
BA = DA - 20.00;
}
}
Now tell me:
Where is the variable S, Single, Married and DA initialized or getting value?
> if ((PID == S) && (MS == Single))
In the above statement, both S and Single are uninitialized.
You are not supposed to use a variable without initialization. It will have junk value and the result is undefined!
-Narendra
Hi Sir,
there is my program it compile and run now but doesnot calculate.Can you please check the calculation
/* this is a Payroll calculation Program*/
# include<stdio.h>
# include<conio.h>
#define Single 1
#define Married 2
#define Deduction 0.17
int main(void)
{
int EN ,H;
float GP,DA,BD,NP,PR,Salary;
char MS,PID,S,I,J,M;
/*For ten employees*/
/*Loop condition*/
{
printf("please enter the employee number");
scanf("%d",&EN);
fflush(stdin);
printf("please enter the employee hours worked");
scanf("%d",&H);
fflush(stdin);
printf("please enter the employee Position ID");
scanf("%c",&PID);
fflush(stdin);
printf ("please enter the employee marital status");
scanf("%c", &MS);
/*there is 17%of Deduction if GrossPay is>100*/
/*calculate GrossPay based on Pay Rate*/
if ((PID == S) && (MS == Single))
{
GP = H * 18.00;
NP =DA - 10.00;
}
else if ((PID == S) && (MS == Married))
{
GP = H * 18.00;
NP = DA - 20.00;
}
else if ((PID == I) && (MS == Single))
{
GP = H * 12.00;
NP = DA - 10.00;
}
else if ((PID == I) && (MS == Married))
{
GP = H * 12.00;
NP = DA - 20.00;
}
if ((PID == J) && (MS == Single))
{
GP = H * 9.00;
NP = DA - 10.00;
}
else if ((PID == J) && (MS == Married))
{
GP = H * 9.00;
NP = DA - 20.00;
}
if ((PID == M) && (MS == Single))
{
GP = H * Salary;
NP = DA - 10.00;
}
else if ((PID == M) && (MS == Married))
{
GP = H * Salary ;
NP = DA - 20.00;
}
else
("THE END");
printf("\nGrossPay:%f,\nDeduction Amount%f,\nBenefit Deduction%1f,\nNetPay%f,&GP,&DA,&BD,&NP");
printf("\nPosition ID%f,Employee Number%d,&PID,&EN");
}
}/*End of loop*/
AnswerStill there are lot of mistakes!
You are using DA in the calculation:
NP = DA - 10.00;
But, where are you giving value to it.
So, it will have junk value and hence the result NP will become junk.
In the comparison statement:
else if ((PID == S) && (MS == Married))
What is the value of S?
And MS & Married are of different types.
Married is having the value 2. But the user would have entered 'M' or 'm' or 'n' or 'N' and you are comparing this with 2!
So, this will never become TRUE!!
And there is a mistake in printf:
else
("THE END");
printf("\nGrossPay:%f,\nDeduction Amount%f,\nBenefit Deduction%1f,\nNetPay%f,&GP,&DA,&BD,&NP");
printf("\nPosition ID%f,Employee Number%d,&PID,&EN");
Change this to:
else
printf("THE END");
printf("\nGrossPay: %f\nDeduction Amount: %f\nBenefit Deduction: %1f\nNetPay: %f\n", &GP, &DA, &BD, &NP);
printf("\nPosition ID: %f,Employee Number: %d\n", &PID, &EN);
Correct all these mistakes and we can debug further after that.
-Narendra