Hi all, I am a student in my first programming class. We are learning C, and so far so good, except for a problem i keep having with scanf. Basically, if I am lookinig f an integer value and someone enters a character, I want the program to alert the error, and loop back. If the user enters an invalid integer it works, but the minute the user enters a character the program ends. any help on overcoming this would be appreciated. Here is a sample problem I am working on out of c primer plus.
/* exersise 8 --
* 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:
Basic pay rate = user enters choice
Overtime (in excess of 40 hours) = time and a half
Tax rate
15% of the first $300
20% of the next $150
25% of the rest
*
*
*/
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int hrs = 0; // the number of hours worked
int othrs = 0; // the number of hrs over 40
float grosspay = 0.0; // gross pay
float netpay = 0.0; // net pay
float txrate1 = 0.15; // rate for first 300
float txrate2 = 0.20; // rate for next 150
float txrate3 = 0.25; // rate for the rest
float taxes = 0.0; // taxes
float basepay = 0.0; // pay rate
int choice =0; // menu choice
// get pay rate
printf("****************************************************************\n");
printf("Enter the number the corresponds with your current rate of pay: \n");
printf("1) $8.75/hr\t\t\t2) $9.33/hr\n");
printf("3) $10.00/hr\t\t\t4) $11.20/hr\n");
printf("5) quit\n");
printf("****************************************************************\n");
scanf(" %d",&choice);
// validate choice
if ((choice > 5))
{
do
{
printf("You did not enter a valid choice, please try again!\n\n");
printf("****************************************************************\n");
printf("Enter the number the corresponds with your current rate of pay: \n");
printf("1) $8.75/hr\t\t\t2) $9.33/hr\n");
printf("3) $10.00/hr\t\t\t4) $11.20/hr\n");
printf("5) quit\n");
printf("****************************************************************\n");
}while((scanf(" %d",&choice) != 1) || (choice > 5));
}
switch(choice)
{
case 1:
basepay = 8.75;
break;
case 2:
basepay = 9.33;
break;
case 3:
basepay = 10.00;
break;
case 4:
basepay = 11.20;
break;
default:
printf("Quitting the program. Good bye!");
exit(0);
}
// get the hours worked
printf("\nIt's time to get paid. ");
printf("Please enter the number of hours you worked this week: ");
scanf(" %d",&hrs);
// calculate normal and ot hours
if(hrs < 40)
{
othrs = 0;
}
else
{
othrs = hrs - 40;
hrs = 40;
}
// calculate the gross pay
grosspay = (hrs * basepay) + (othrs * basepay * 1.5);
// calculate taxes
if (grosspay <= 300.00)
{
taxes = grosspay * txrate1;
netpay = grosspay - taxes;
}
else if (grosspay <= 450.00)
{ taxes = ((grosspay - 300)*txrate2) + (300.00*txrate1);
netpay = grosspay - taxes;
}
else
{
taxes = ((grosspay - 450.00)*txrate3) + (150.00*txrate2) + (300.00*txrate1);
netpay = grosspay - taxes;
}
// display the results
printf("Gross Pay:\t\t\t%.2f\n",grosspay);
printf("Taxes :\t\t\t%.2f\n",taxes);
printf("Net Pay :\t\t\t%.2f\n",netpay);
return 0; // end program
}