使用 gets() 和 printf 的代码不起作用
Code with gets() and printf not working
所以,我正在尝试根据用户的性别来执行 "Hello Mr" 或 "Hello Mrs" 的代码,但是当我 运行 程序时,它不会不让我输入我的名字,但为什么?
此外,我尝试使用 fgets(),但编译器显示“
函数参数太少 'fgets' "
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void flushstdin()
{
int c;
while((c=getchar())!= '\n' && c != EOF);
}
int main () {
float sex;
char name[60];
printf("\nInform your sex: 1 if you are male, 2 if you are female.");
while(scanf("%f",&sex)!=1 || sex!=1 && sex!=2){ //In case the person typed something different of 1,2.
printf("\nInform a correct value, 1 or 2.\n");
flushstdin();
}if(sex==1){
printf("Inform your name.\n");
gets(name);
printf("\nHello Mr. %s \n",name);
}
if(sex==2){
printf("Inform your name.\n");
gets(name);
printf("\nHello Mrs. %s \n",name);
}
system("pause");
return 1;
}
在这种情况下,当回车传递用户是女性还是男性的数据时,输入字符'\n'仍然在输入缓冲区中的队列中。使用 scanf 时会发生这种情况。这意味着后面的 gets() 函数将读取仍在缓冲区中的 '\n' 字符,而无需先询问用户。
一个简单的解决方案是在询问将接收缓冲区中剩余输入的用户性别后添加两行代码:
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void flushstdin() {
int c;
while((c=getchar())!= '\n' && c != EOF);
}
int main () {
float sex;
char name[60];
printf("\nInform your sex: 1 if you are male, 2 if you are female.");
while(scanf("%f",&sex)!=1 || sex!=1 && sex!=2){ //In case the person typed something different of 1,2.
printf("\nInform a correct value, 1 or 2.\n");
flushstdin();
}
//new code, extracts input from buffer until it reads a '\n' character or buffer is empty
char c;
while(( c = getchar()) != '\n' && c != EOF);
//end of new code
if(sex==1){
printf("Inform your name.\n");
gets(name);
printf("\nHello Mr. %s \n",name);
}
if(sex==2){
printf("Inform your name.\n");
gets(name);
printf("\nHello Mrs. %s \n",name);
}
system("pause");
return 1;
}
所以,我正在尝试根据用户的性别来执行 "Hello Mr" 或 "Hello Mrs" 的代码,但是当我 运行 程序时,它不会不让我输入我的名字,但为什么?
此外,我尝试使用 fgets(),但编译器显示“ 函数参数太少 'fgets' "
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void flushstdin()
{
int c;
while((c=getchar())!= '\n' && c != EOF);
}
int main () {
float sex;
char name[60];
printf("\nInform your sex: 1 if you are male, 2 if you are female.");
while(scanf("%f",&sex)!=1 || sex!=1 && sex!=2){ //In case the person typed something different of 1,2.
printf("\nInform a correct value, 1 or 2.\n");
flushstdin();
}if(sex==1){
printf("Inform your name.\n");
gets(name);
printf("\nHello Mr. %s \n",name);
}
if(sex==2){
printf("Inform your name.\n");
gets(name);
printf("\nHello Mrs. %s \n",name);
}
system("pause");
return 1;
}
在这种情况下,当回车传递用户是女性还是男性的数据时,输入字符'\n'仍然在输入缓冲区中的队列中。使用 scanf 时会发生这种情况。这意味着后面的 gets() 函数将读取仍在缓冲区中的 '\n' 字符,而无需先询问用户。
一个简单的解决方案是在询问将接收缓冲区中剩余输入的用户性别后添加两行代码:
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void flushstdin() {
int c;
while((c=getchar())!= '\n' && c != EOF);
}
int main () {
float sex;
char name[60];
printf("\nInform your sex: 1 if you are male, 2 if you are female.");
while(scanf("%f",&sex)!=1 || sex!=1 && sex!=2){ //In case the person typed something different of 1,2.
printf("\nInform a correct value, 1 or 2.\n");
flushstdin();
}
//new code, extracts input from buffer until it reads a '\n' character or buffer is empty
char c;
while(( c = getchar()) != '\n' && c != EOF);
//end of new code
if(sex==1){
printf("Inform your name.\n");
gets(name);
printf("\nHello Mr. %s \n",name);
}
if(sex==2){
printf("Inform your name.\n");
gets(name);
printf("\nHello Mrs. %s \n",name);
}
system("pause");
return 1;
}