在浪费了太多时间搜索为什么我的程序在使用 scanf() 后不执行 gets() 之后,我找到了一个解决方案,即在 scanf() 之后使用 fflush(stdin) 来启用 gets() 以获取字符串。
问题是 fflush(stdin) 没有达到预期的效果:程序继续跳过gets(),我无法在控制台中写入任何要读取的短语。
我的代码是下一个:
#include <string.h>
#include <stdio.h>
int main(){
char nombre[10];
char mensaje[80];
printf("Type your name:\n");
scanf("%s", nombre);
fflush(stdin);
printf("Now, type a message:\n");
gets(mensaje);
printf("3/%s:%s",nombre,mensaje);
return 0;
}