C Primer Plus Exercise ch7.08
Hi, I got stuck at the 8th exercise of ch7 in the book C Primer Plus. The requirement is (it is a follow up of exercise No7, so I will paste both No7 and No8 below):
7. Write a program that requests the hours worked in a week and then prints the gross pay,the taxes, and the net pay. Assume the following:
a. Basic pay rate = $10.00/hr
b. Overtime (in excess of 40 hours) = time and a half
c. Tax rate: #15% of the first $300
20% of the next $150
25% of the rest
Use #define constants, and don’t worry if the example does not conform to currenttax law.
8. Modify assumption a. in exercise 7 so that the program presents a menu of pay ratesfrom which to choose. Use a switch to select the pay rate. The beginning of a runshould look something like this:
************************************************** ***************
Enter the number corresponding to the desired pay rate or action:
1) $8.75/hr 2) $9.33/hr
3) $10.00/hr 4) $11.20/hr
5) quit
************************************************** ***************
If choices 1 through 4 are selected, the program should request the hours worked. Theprogram should recycle until 5 is entered. If something other than choices 1 through 5is entered, the program should remind the user what the proper choices are and thenrecycle. Use #defined constants for the various earning rates and tax rates.
And this is my code:
Code:
#include <stdio.h>
#define RATE1 8.75
#define RATE2 9.33
#define RATE3 10.00
#define RATE4 11.20
#define TAX1 0.15
#define TAX2 0.2
#define TAX3 0.25
#define TAX1_LEVEL 300
#define TAX2_LEVEL TAX1_LEVEL + 150
#define TAX1_FULL TAX1 * TAX1_LEVEL
#define TAX2_FULL TAX2 * 150 + TAX1_FULL
int main(void)
{
float rate, hours, gross, tax, net;
int m, choice;
// This is the input menu.
printf("*****************************************************************\n");
printf("Enter the number corresponding to the desired pay rate or action:\n");
printf("1) $8.75/hr 2) $8.33/hr\n");
printf("3) $10.00/hr 4) $11.20/hr\n");
printf("5) quit\n");
printf("*****************************************************************\n");
m = scanf("%d", &choice);
while(m != 1)
{
printf("Please input an integer between 1 to 5.\n");
m = scanf("%d", &choice);
}
while(m == 1)
{
switch (choice)
{
case 1 : rate = RATE1;
break;
case 2 : rate = RATE2;
break;
case 3 : rate = RATE3;
break;
case 4 : rate = RATE4;
break;
case 5 : goto quit; //quit the whole programme
default: printf("Please input a number from 1 to 5 to choose!\n");
goto enteragain; //jump over the calculations to enter rate again
}
printf("You've chosen the rate number%d: %.2f.\n", choice, rate);
//enter the worked hours
printf("\nNow enter the hours worked in a week: \n");
scanf("%f", &hours);
//determine the gross pay
if (hours <= 40)
gross = rate * hours;
else
gross = rate * 40 + 1.5 * rate * (hours - 40);
//determine the tax value
if (gross <= TAX1_LEVEL)
tax = gross * TAX1;
else if (gross <= TAX2_LEVEL)
tax = TAX1_FULL + (gross - TAX1_LEVEL) * TAX2;
else
tax = TAX2_FULL + (gross - TAX2_LEVEL) * TAX3;
//net pay
net = gross - tax;
//output
printf("\nThe gross pay is %.2f.\nTaxes : %.2f.\nNet pay is %.2f.\n\n", gross, tax, net);
//enter again
printf("Please enter your choice again: \n");
enteragain: m = scanf("%d", &choice);
while(m != 1)
{
printf("Please input an integer between 1 to 5.\n");
m = scanf("%d", &choice);
}
}
quit: printf("Bye.\n");
return 0;
}
Compiled and ran. Works fine if I input an integer as required. But if a non-integer value was inputted, I had the "Please input an integer between 1 to 5." running non-stop and I can't type in any other input. Anyone can give me a hint or any suggestion to improve my code?