onefootswill

onefootswill's Avatar, Join Date: Nov 2007
Newbie Member
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.



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
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
You have submitted it as an Article and I have moved to the forum for discussion.
0
onefootswill's Avatar, Join Date: Nov 2007
Newbie Member
This is now solved. Thanks.