*编译器特定的*构建和运行给出输出错误

时间:2016-01-09 16:15:51

标签: c visual-studio-2013 codeblocks

当我在 Visual Studio 2013上运行以下代码时:

#include<stdio.h>
#include<ctype.h>

int main(void) {
    char ch;
    printf("Enter an alphabet: ");
    scanf_s("%c",&ch);
    switch(tolower(ch)) {
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':       
            printf("Alphabet is a vowel\n");
            break;
        default:
            printf("Alphabet is a consonant\n");
    }
    system("pause");
    return 0;
}

...输出错误为:

Enter an alphabet : a
Alphabet is a consonant
Press any key to continue . . .

对于开关中的任何字母选项,此错误相同。 但是相同的代码在 CodeBlocks

中提供了正确的输出
Enter an alphabet : a
Alphabet is a vowel

Process returned 0 (0x0)   execution time : 3.824 s
Press any key to continue.

1 个答案:

答案 0 :(得分:1)

尝试使用scanf

scanf (" %c", &ch);

请注意,这里的缩进

"

%c

因为如果没有缩进,可能会导致一些错误。