# Thread: new to c. problem with exercise.

1. ## new to c. problem with exercise.

I just started working with C and I'm reading C How to program. I really enjoy this book over all other programming books that I've read simply because this book has numerous exercises at the end of the chapters. This is helping me to really make sure that I have learned what the chapter was trying to teach. However, I've now run into a problem with some of the exercises. This one in particular has me stumped. The basic problem is that you are first supposed to find out what type of employee the person is (number from 1 to 4). If 1 (a manager) you receive a flat weekly salary. No problem there. If 2 (hourly employee) you also have to input your hours for the week and hourly rate. Then the program is supposed to output their weekly pay based off hours plus any possible overtime. There are 2 other employee types, but I haven't even gotten there yet because I'm having trouble calculating the hourly wage for employee 2. It keeps giving me back the results that I made 0.00 this week. Can anyone see from my code what I'm doing wrong? Thanks.

#include <stdio.h>

int main()
{

int empType = 0, hoursWorked = 0;
double hourlyRate = 0, overTime = 0;

printf("What type of employee are you?");
scanf("%d",&empType);

switch (empType){

case 1:
//manager
printf("You have made \$500.00 this week.\n");
break;
case 2:
//hourly
printf("Please enter the number of hours you worked.");
scanf("%d", &hoursWorked);
scanf("%f", &hourlyRate);
if ( hoursWorked > 0 )
{
overTime = (double)((hoursWorked - 40) * (hourlyRate * 1.5));
}
printf("You made %.2f this week.\n", overTime);
break;
case 3:
//commission
break;
case 4:
//pieceworkers
break;
default:
printf("Sorry, I don't have functions written for that type of employee.");
break;
}

return 0;
}

2. case 2:
//hourly
printf("Please enter the number of hours you worked.");
scanf("%d", &hoursWorked);
scanf("%f", &hourlyRate);
if ( hoursWorked > 0 )
{
overTime = (double)((hoursWorked - 40) * (hourlyRate * 1.5));
}
printf("You made %.2f this week.\n", overTime);
break;

If you enter 40 as the number of hours worked, you'll end up with zero because of your multiplication. You need to first test to see if the hours worked is over 40 hours, something like:
if (hoursWorked>40){
overTime = (double)((hoursWorked - 40) * (hourlyRate * 1.5));
totalTime = double((40*hourlyRate)+overTime);
}
else {totalTime = double(hoursWorked*hourlyRate);}

and then print out totalTime instead of overTime. Hope that helps.

3. ## I tried to modify it but it didn't change the results

I made your recommended modifications, but it didn't change the output. I also started working on the 3rd type and it's also not outputting correct data, so now I'm really stumped. Here's my revised code.

#include <stdio.h>

int main()
{

int empType = 0, hoursWorked = 0;
double hourlyRate = 0, overTime = 0, commissionRate = 250.00, salesAmount = 0, totalAmount = 0;

printf("What type of employee are you?");
scanf("%d",&empType);

switch (empType){

case 1:
//manager
printf("You have made \$500.00 this week.\n");
break;
case 2:
//hourly
printf("Please enter the number of hours you worked.");
scanf("%d", &hoursWorked);
scanf("%f", &hourlyRate);
if ( hoursWorked > 40 )
{
overTime = (double)((hoursWorked - 40) * (hourlyRate * 1.5));
totalAmount = (double)((40* hourlyRate) + overTime);
}
else
{
totalAmount = double(hoursWorked * hourlyRate);
}
printf("You made %.2f this week.\n", totalAmount);
break;
case 3:
//commission
scanf("%f", &salesAmount);
printf("Your pay is %.2f\n", 250.00 + (salesAmount * .057));
break;
case 4:
//pieceworkers
break;
default:
printf("Sorry, I don't have functions written for that type of employee.");
break;
}

return 0;
}

4. ## Nevermind

I figured out what the problem was. I was using the %f format specifier in the scanf function call when I was assigning a value to a double. I learned later in the next chapter that I have to use the %lf specifier to correctly assign the value to a double. Thanks.

5. ## Please use code tags.

[ c o d e ]

.. place code here

[ / c o d e ]

Example:
Code:
```#include <stdio.h>

int main()
{
printf("Hello, world.");
return 0;
}```