bakaSuc 2021-09-22 16:08 采纳率: 66.7%
浏览 46
已结题

一个简单的scanf的参数报错问题

程序能正常运行,但为什么这里会报错?

//检测用户是不是只输入了一个大写字母
#include <stdio.h>
#include <ctype.h>
int main(void)
{
    char ch;

    printf("Please enter an uppercase letter to start:");
    while (scanf_s("%c", &ch)) //此行报了黄色警告
    {
        if (ch == '\n') //检测首字符是否为换行符
        {
            printf("Please enter an uppercase letter!\n");
            continue;
        }
        else if (getc(stdin) != '\n') //检测是否有多余的字符
        {
            printf("Please enter an uppercase letter!\n");
            while (getchar() != '\n')
                ;
            continue;
        }
        else if (!isupper(ch)) //检测是否为大写字母
        {
            printf("Please enter an uppercase letter!\n");
            continue;
        }
        printf("Done");
    }

    return 0;
}

img

  • 写回答

2条回答 默认 最新

  • Pliosauroidea 2021-09-22 16:09
    关注

    scanf_s("%c", &ch,1)试试

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 9月30日
  • 已采纳回答 9月22日
  • 创建了问题 9月22日