Switch语句始终包括case和default

时间:2015-01-07 22:13:17

标签: c

当我编译这个程序时,它输出相应的开关盒和默认标签内容,它唯一没有打印出来的值是1月,任何帮助都会很棒!

样本输入:4

示例输出:

四月 错误

虽然我可以让程序更好,但我需要以特定的方式使用它。

while (month != EOF)
{
        switch (month)
        {

            case 49:
            month2 = getchar();

            switch (month2)
            {
                case 10:
                    printf("January \n");
                    break;
                case 48:
                    printf("October \n");
                    break;
                case 49:
                    printf("November \n");
                    break;
                case 50:
                    printf("December \n");
                    break;
            }
            break;

                case 50:
                    printf("February \n");
                    break;
                case 51:
                    printf("March \n");
                    break;
                case 52:
                    printf("April \n");
                    break;
                case 53:
                    printf("May \n");
                    break;
                case 54:
                    printf("June \n");
                    break;
                case 55:
                    printf("July \n");
                    break;
                case 56:
                    printf("August \n");
                    break;
                case 57:
                    printf("Septembe \n");
                    break;
                default: printf("Error \n");





            }
            month = getchar();
}


system("PAUSE");
return (0);

}

1 个答案:

答案 0 :(得分:1)

你只需要一个额外的案件来处理流浪' \ n'来自你在你的终端按Enter键。如果您只输入1

,则1月应该有效

编辑:实际上我只是在Linux上测试它并且它完美无缺,但在Windows上可能会有一些细微差别。

while (month != EOF)
{
        switch (month)
        {

            case 49:
            month2 = getchar();

            switch (month2)
            {
                case 10:
                    printf("January \n");
                    break;
                case 48:
                    printf("October \n");
                    break;
                case 49:
                    printf("November \n");
                    break;
                case 50:
                    printf("December \n");
                    break;
            }
            break;

                case 50:
                    printf("February \n");
                    break;
                case 51:
                    printf("March \n");
                    break;
                case 52:
                    printf("April \n");
                    break;
                case 53:
                    printf("May \n");
                    break;
                case 54:
                    printf("June \n");
                    break;
                case 55:
                    printf("July \n");
                    break;
                case 56:
                    printf("August \n");
                    break;
                case 57:
                    printf("Septembe \n");
                    break;
                // Filter out stray \n
                case '\n':
                    break;
                default: printf("Error \n");





            }
            month = getchar();
}


system("PAUSE");
return (0);