Reputation: 1
I am trying to make a program that prompts the user a menu of options 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
*****************************************************************
And then calculate the net pay, gross pay and tax, according to the pay rate. I have all the pay rates done but the actual menu of rates is giving me problems.If the user enter 5 it should quit and if enters anything other and 1 through 5 then recycle and ask again for proper choice. I have a problem recycling if input is any other than 1 through 5.
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include "ctype.h"
#define HOURLY0 8.75
#define HOURLY1 9.33
#define HOURLY2 10
#define HOURLY3 11.20
#define TAXRATE .15
#define TAXRATE2 .20
#define TAXRATE3 .25
#define OVERTIME 15
int _tmain(int argc, _TCHAR* argv[])
{
float hours, grossPay = 0, netPay, tax = 0, tax3 = 0,hourly;
int menu = 0,wrong =1;
char quit;
printf("Enter the number corresponding to the desired pay rate or action : \n\n1) $8.75/hr 2) $9.33/hr \n3) $10.00/hr 4) $11.20/hr \n5) quit\n\n");
scanf_s("%d", &menu);
while (menu != 5 )
{
do
{
switch (menu)
{
case 1:
hourly = HOURLY0;
break;
case 2:
hourly = HOURLY1;
break;
case 3:
hourly = HOURLY2;
break;
case 4:
hourly = HOURLY3;
break;
default:
printf("Enter right choice from 1 to 5 only\n");
printf("Enter the number corresponding to the desired pay rate or action : \n\n1) $8.75/hr 2) $9.33/hr \n3) $10.00/hr 4) $11.20/hr \n5) quit\n\n");
scanf_s("%d", &menu);
wrong;
break;
}
} while (!wrong);/* HOW CAN I MAKE IT TO RECYCLE IF INPUT IS OTHER THAN 1-5*/
printf("\nEnter hours worked in the week: ");
scanf_s("%f", &hours);
if (hours > 40)
{
grossPay = (40 * hourly) + ((hours - 40) * OVERTIME);
if (grossPay <= 300)
{
tax = grossPay * TAXRATE;
}
if (grossPay > 300 && grossPay < 450)
{
tax = (300 * TAXRATE) + ((grossPay - 300)*TAXRATE2);
}
if (grossPay > 450)
{
tax3 = grossPay - 450;
tax = (300 * TAXRATE) + ((grossPay - 300 - tax3)*TAXRATE2) + ((grossPay - 300 - 150)*TAXRATE3);
}
}
else if (hours < 40)
{
grossPay = (hours * hourly);
if (grossPay <= 300)
{
tax = grossPay * TAXRATE;
}
if (grossPay > 300 && grossPay < 450)
{
tax = (300 * TAXRATE) + ((grossPay - 300)*TAXRATE2);
}
if (grossPay > 450)
{
tax3 = grossPay - 450;
tax = (300 * TAXRATE) + ((grossPay - 300 - tax3)*TAXRATE2) + ((grossPay - 300 - 150)*TAXRATE3);
}
}
netPay = grossPay - tax;
printf("\nGross Pay : %2.3f\nTax: %13.3f\nNet Pay: %10.3f\n\n", grossPay, tax, netPay);
system("cls");
printf("Enter the number corresponding to the desired pay rate or action : \n\n1) $8.75/hr 2) $9.33/hr \n3) $10.00/hr 4) $11.20/hr \n5) quit\n\n");
scanf_s("%d", &menu);
}
system("pause");
return 0;
}
Upvotes: 0
Views: 3498
Reputation: 34839
One way to improve the code is to move user input processing into a subroutine. That way, all of the messy error handling goes into the subroutine and main
only has to process valid inputs.
In the code below, the GetUserInput
function will loop forever, until either the user enters a valid number, or an end-of-file or error occurs in the scanf
. The return value from GetUserInput
can only be a value from 1 thru 5, so main
doesn't need to handle any unexpected values.
int GetUserInput( void )
{
int menu;
for (;;)
{
menu = 0;
printf("Enter the number corresponding to the desired pay rate or action : \n\n1) $8.75/hr 2) $9.33/hr \n3) $10.00/hr 4) $11.20/hr \n5) quit\n\n");
if ( scanf( "%d", &menu ) != 1 )
exit( 1 );
if ( menu >= 1 && menu <= 5 )
return menu;
printf( "Enter right choice from 1 to 5 only\n" );
}
}
int main( void )
{
int menu = 0;
while ( menu != 5 )
{
menu = GetUserInput();
switch ( menu )
{
case 1: printf( "\n*** You selected 1 ***\n\n" ); break;
case 2: printf( "\n*** You selected 2 ***\n\n" ); break;
case 3: printf( "\n*** You selected 3 ***\n\n" ); break;
case 4: printf( "\n*** You selected 4 ***\n\n" ); break;
case 5: printf( "Bye\n" ); break;
}
}
}
Upvotes: 2
Reputation: 337
After the while(!wrong) loop, were you have put your comment, add:
if(wrong)
continue; //restart from the outer-while
and initiate wrong=0 at the begining and set it to 1 at the default choice of switch.
Upvotes: 0
Reputation: 36102
When you use scanf()
you need to be aware of that when a user enters a value scanf tries to read out only what you write in the format specifier, the rest still remains in the buffer. So writing "%d" means it reads the number but left in the buffer is \n.
Now when you later do another scanf the \n is still in the buffer so scanf directly returns 0.
Instead use fgets()
to read the value from the keyboard into a character string, then use sscanf()
or atoi()
to retrieve the integer.
e.g.
char buffer[256];
while (fgets(buffer,sizeof(buffer),stdin)!=NULL)
{
if (sscanf(buffer, "%d", &n) == 1)
{
switch(n) {...}
}
else
{
... some error output ...
}
}
Upvotes: 0