gpt4 book ai didi

c - 开关盒菜单

转载 作者:太空宇宙 更新时间:2023-11-04 08:29:43 25 4
gpt4 key购买 nike

我正在尝试制作一个程序,向用户提示这样一个选项菜单

*****************************************************************
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
*****************************************************************

然后根据工资率计算净工资、毛工资和税金。我已经完成了所有的工资率,但实际的费率菜单给我带来了问题。如果用户输入 5,它应该退出,如果输入任何其他内容和 1 到 5,然后再循环并再次询问正确的选择。如果输入不是 1 到 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;
}

最佳答案

改进代码的一种方法是将用户输入处理移到子例程中。这样一来,所有困惑的错误处理都进入了子例程,main 只需处理有效输入。

在下面的代码中,GetUserInput 函数将永远循环,直到用户输入有效数字、文件结束或 scanf 发生错误>。 GetUserInput 的返回值只能是 1 到 5 之间的值,因此 main 不需要处理任何意外值。

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;
}
}
}

关于c - 开关盒菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28973004/

25 4 0