While循环打印和scanf

While循环打印和scanf,c,scanf,C,Scanf,此程序具有此值: ABCDEF 一开始还可以吗 但同样,当您输入一个带有空格的值时,如: ABC DEF 程序运行错误!!!! while循环第二次忽略scanf 我做错了什么 #include <stdio.h> #include <stdlib.h> #include <stdbool.h> int main(){ bool checkSrc = false; bool checkDst = false; while (!chec

此程序具有此值: ABCDEF 一开始还可以吗

但同样,当您输入一个带有空格的值时,如: ABC DEF 程序运行错误!!!! while循环第二次忽略scanf 我做错了什么

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main(){
    bool checkSrc = false;
    bool checkDst = false;

    while (!checkSrc && !checkDst)

    {

    char ins[10];

    printf("White Player : ");
    scanf("%s",&ins);

    }

}
#包括
#包括
#包括
int main(){
bool checkSrc=false;
bool-checkDst=false;
而(!checkSrc&!checkDst)
{
char-ins[10];
printf(“白人玩家:”);
scanf(“%s”、&ins);
}
}
%s-字符串。这将读取后续字符,直到找到空白(空白字符被视为空白、换行和制表符)

我建议您使用
fgets()
而不是
scanf()
,因为后者没有缓冲区溢出保护

#define namesize 15
char *ins = malloc (namesize);

fgets(ins, namesize, stdin);

scanf
格式
“%s”
读取以空格分隔的“单词”。@user3121023是,但也需要在
ins[strlen(ins)-1]
处检查
'\n'
字符。如果不存在,则说明缓冲区太小,并且输入行上还有未读字符。如果存在,则缓冲区足够大,您可以对字符串执行进一步的操作,例如剥离前导和尾随空白字符,包括最后一个换行字符……请注意,新行字符'\n',如果有,将进入缓冲区
ins