程序无法编译fgets
在 teste1
功能。或者至少它不能正常工作,它不会让我输入字符串,程序将在打印“Nome do cliente”后立即结束。
如果我禁用其他 scanf
在函数中,它将运行没有任何问题。
如何让 fgets 工作?
#include <stdio.h>
void teste1(){
char teste[50];
printf("Nome do cliente\n");
fgets(teste,50,stdin);
}
void teste2(){
teste1();
}
void teste3(){
int opc1,opc2;
printf("\nSeleccione a área desejada\n1- Clientes\n2- Concessionários\n3- Carros de demonstração\n");
scanf("%d",&opc1);
printf("\nSeleccione a área desejada\n1- Inserir\n2- Alterar\n3- Remover\n4- Consultar\n");
scanf("%d",&opc2);
teste2();
}
int main()
{
teste3();
}
最佳答案
您在最后一次 scanf
输入中按下的 Enter 键将作为换行符保留在输入缓冲区中。 fgets
函数将读取此换行符并认为它已完成。
关于c - fgets 可能会由于其他 scanf 的原因而被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41831398/