onefootswill

Discussion in 'C' started by onefootswill, Nov 19, 2007.

  1. onefootswill

    onefootswill New Member

    Joined:
    Nov 19, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Brisbane, Australia
    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.

    [​IMG]

    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
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    You have submitted it as an Article and I have moved to the forum for discussion.
     
  3. onefootswill

    onefootswill New Member

    Joined:
    Nov 19, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Brisbane, Australia
    This is now solved. Thanks.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice