切换案例菜单

时间:2015-03-10 19:59:07

标签: c

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

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

3 个答案:

答案 0 :(得分:2)

改进代码的一种方法是将用户输入处理移动到子例程中。这样,所有混乱的错误处理都进入子程序,而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;
        }
    }
}

答案 1 :(得分:0)

当您使用scanf()时,您需要注意当用户输入值时scanf尝试仅读出您在格式说明符中写入的内容,其余内容仍保留在缓冲区中。所以写&#34;%d&#34;意味着它读取数字但留在缓冲区中的是\ n。

现在当你以后再做一次scanf时,\ n仍然在缓冲区中,所以scanf直接返回0.

而是使用fgets()将键盘中的值读入字符串,然后使用sscanf()atoi()来检索整数。

e.g。

char buffer[256];
while (fgets(buffer,sizeof(buffer),stdin)!=NULL)
{
  if (sscanf(buffer, "%d", &n) == 1)
  {
    switch(n) {...}
  }
  else
  {
    ... some error output ...
  }
}

答案 2 :(得分:0)

while(!wrong)循环之后,您是否已发表评论,请添加:

if(wrong)
    continue; //restart from the outer-while

并在开始时启动错误= 0 ,并在默认选择开关时将其设置为 1