Comparing user-inputted characters in C

前端 未结 4 1557
失恋的感觉
失恋的感觉 2020-12-30 04:47

The following code snippets are from a C program.

The user enters Y or N.

char *answer = \'\\0\';

scanf (\" %c\", answer);

if (*answer == (\'Y\' ||         


        
4条回答
  •  孤城傲影
    2020-12-30 05:15

    answer shouldn't be a pointer, the intent is obviously to hold a character. scanf takes the address of this character, so it should be called as

    char answer;
    scanf(" %c", &answer);
    

    Next, your "or" statement is formed incorrectly.

    if (answer == 'Y' || answer == 'y')
    

    What you wrote originally asks to compare answer with the result of 'Y' || 'y', which I'm guessing isn't quite what you wanted to do.

提交回复
热议问题