Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   onefootswill (http://www.go4expert.com/forums/onefootswill-t7382/)

onefootswill 19Nov2007 12:26

onefootswill
 
I have no idea if C newbies are welcome here, so I have a novice problem which I cannot figure out. This is just an exercise from a book. There are two problems:

1st: as per following screenshot, the menu is displayed twice, following the 1st operation.

http://i167.photobucket.com/albums/u...nd/payroll.png

2nd: the calculation is wrong for hourly workers who worked more than 40 hours. They should be paid 1.5 times the hourly rate for each hour over 40 hours.

The code is:
Code:

#include <stdio.h>

int main()
{
        char ch;
        float weeklyPay;


        printf("Select the relevant employee type (press ctrl-z to exit): \n");
        printf("m (manager)\n");
        printf("h (hourly workers)\n");
        printf("c (commission workers)\n");
        printf("p (piece-workers)\n");

        ch = getchar();

        while(ch != EOF)
        {
                switch(ch)
                {
                case 'm': printf("Enter the manager's salary: \n");
                        int salary;
                                scanf("%d", &salary);
                                weeklyPay = (float)salary / 52;
                                printf("Manager's weekly pay is: $%.2f", weeklyPay);
                                break;
                               

                case 'h': printf("Enter the number of hours worked: ");
                        int hours = 0;
                        float hourlyRate = 0;

                        scanf("%i", &hours);
                        printf("Enter the hourly rate: ");
                        scanf("%f", &hourlyRate);
                               
                       
                        if(hours < 41)
                                weeklyPay = (float)hours * hourlyRate;
                       
                        else
                                weeklyPay = (float)(40 * hourlyRate) + ((40 - hours) * 1.5 * hourlyRate);
                       
                        printf("Hourly worker's weekly pay was $%.2f\n\n", weeklyPay);
                        break;                       
                       
                }

                printf("Select the relevant employee type (press ctrl-z to exit): \n");
                printf("\tm (manager)\n");
                printf("\th (hourly workers)\n");
                printf("\tc (commission workers)\n");
                printf("\tp (piece-workers)\n");

                ch = getchar();
        }

        return 0;
}

Thanks

shabbir 19Nov2007 18:23

Re: onefootswill
 
You have submitted it as an Article and I have moved to the forum for discussion.

onefootswill 19Nov2007 19:35

Re: onefootswill
 
This is now solved. Thanks.


All times are GMT +5.5. The time now is 12:12.