案例循环遇到问题

时间:2014-08-14 03:52:01

标签: c case

请。我想要一个显示来自' A'到' H'但是"默认无效选项"不断出现。如何防止"默认无效选项出现。非常感谢,如果你能帮我一把。

void disp3Ave (void)
{
    int day, total;
    char j;
    extern float PSI23ave[];
    extern float PSI24ave[];
    extern float PSI25ave[];
    float average;
    total=0;
    printf("Day 23 or 24?\n");
    scanf("%\d", &day);

    if(day == 23)
    {
        printf("A: 0300.\n");
        printf("B: 0600.\n");
        printf("C: 0900.\n");
        printf("D: 1200.\n");
        printf("E: 1500.\n");
        printf("F: 1800.\n");
        printf("G: 2100.\n");
        printf("H: 0000.\n");
        scanf("%c", &j);

        switch (j)
        {
            case 'a':
            case 'A': total= PSI23ave[3] + PSI23ave[4] + PSI23ave[5];
                  average = total/3;
                  printf("3-hr average = %.1f\n", average);
                  break;
            case 'b':
            case 'B': for (j=0;j<24;j++) total= PSI23ave[6] + PSI23ave[7] + PSI23ave[8];
                  average = total/3;
                  printf("3-hr average = %.1f\n", average);
                  break;
            case 'c':
            case 'C': for (j=0;j<24;j++) total= PSI23ave[9] + PSI23ave[10] + PSI23ave[11];
                  average = total/3;
                  printf("3-hr average = %.1f\n", average);
                  break;
            case 'd':
            case 'D': for (j=0;j<24;j++) total= PSI23ave[12] + PSI23ave[13] + PSI23ave[14];
                  average = total/3;
                  printf("3-hr average = %.1f\n", average);
                  break;
            case 'e':
            case 'E': for (j=0;j<24;j++) total= PSI23ave[15] + PSI23ave[16] + PSI23ave[17];
                  average = total/3;
                  printf("3-hr average = %.1f\n", average);
                  break;
            case 'f':
            case 'F': for (j=0;j<24;j++) total= PSI23ave[18] + PSI23ave[19] + PSI23ave[20];
                  average = total/3;
                  printf("3-hr average = %.1f\n", average);
                  break;
            case 'g':
            case 'G': for (j=0;j<24;j++) total= PSI23ave[21] + PSI23ave[22] + PSI23ave[23];
                  average = total/3;
                  printf("3-hr average = %.1f\n", average);
                  break;
            case 'h':
            case 'H': for (j=0;j<24;j++) total= PSI24ave[0] + PSI24ave[1] + PSI24ave[2];
                  average = total/3;
                  printf("3-hr average = %.1f\n", average);
                  break;

            default: printf("Invalid option.\n");
                 break;

        }
    }

提前致谢!干杯!

2 个答案:

答案 0 :(得分:3)

当您输入int变量day的值时,字符'\n'仍然在输入缓冲区中。
这将存储在您的变量j中,因此您将获得无效选项。

编辑: 您可以尝试scanf("\n%c", &j);

答案 1 :(得分:0)

当user1990169结束时,问题是缓冲区从stdin读取整数,但是换行符仍然在输入缓冲区中。这可以通过多种方式解决,但最简单的方法是添加对getchar()的调用,该调用将从输入流中读取一个字符。

scanf("%d", &day); // remove the \ before d
getchar();

这是一个小程序来演示:

int main(void) {
  int day = -1;
  char c = 'x';
  scanf("%d", &day);
  getchar(); // this line
  scanf("%c", &c);
  printf("read: %d %c\n", day, c);
}

如果你删除对getchar()的调用,你会发现在阅读该字符之前没有等待,它会立即在c变量中读取换行符。

使用isspaceungetc处理由未知数量的空格分隔的输入有更多高级选项,但对于您的简单程序,这应该足够了。

此外,您可以转换字符并且每个字符只有一个案例标签,而不是大写和小写的大小写:

j = toupper(j); // #include <ctype.h>
相关问题