获取(串#)函数跳过第一个获得请求 [英] Gets(string#) function skipping first gets request

查看:152
本文介绍了获取(串#)函数跳过第一个获得请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作我个人的休闲和学习的项目。部分原因是这样的:

I'm working on a project for my own personal leisure and learning. Part of it looks like this:

 #include<stdio.h>
 #include<string.h>
 wgame()
 {
 char string3[12], string2[12], string1[12], string4[12], string5[12];
 memset (string1, 0, 11);
 memset (string2, 0, 11);
 memset (string3, 0, 11);
 memset (string4, 0, 11);
 memset (string5, 0, 11);
 printf("reference C correct\n");
 printf("Okay, so you want a game. Here's one for you\n\n\n");
 printf("This is a word game.\n\n   A noun is a person place or thing.\n   A verb is 
 something that you can get up and do.\n   A subject is what the conversation is about.\n");
 printf("Go ahead, type a subject:\n");
 gets(string3);
 printf("That's a good one. Now, type a verb:\n");
 gets(string2);
 printf("How about another:\n");
 gets(string4);
 printf("Really? Okay. Now, type in a noun:\n");
 gets(string1);
 printf("Cool. How about typing another noun:\n");
 gets(string5);
 printf("Allright, here's how your words fit into this game:\n\n\n\n\n");
 printf("When the %s was %s the %s %s all the other %s", string1, 
 string2, string3, string4, string5);
 return 4;

 }

我的问题是输出跳过第一个获取(串#),并继续到
接下来的printf()。谁能告诉我这是为什么?

My problem is that the output is skipping over the first "gets(string#)" and proceeding to the next "printf()". Can someone tell me why this is?

推荐答案

这可能是因为 wgame 你正在做一些之前 scanf函数留下一个 \\在标准输入输出缓冲器n

It's likely that before wgame you are doing some scanf that leaves a \n in the stdio buffer.

下面是你应该做的几件事情:

Here are a few things you should do:


  • 请不要混用 scanf函数获得

  • 请不要使用获得。使用与fgets

  • 不要听人提示 fflush(标准输入)。这是错误

  • Don't mix scanf and gets
  • Don't use gets. Use fgets
  • Don't listen to people suggesting fflush(stdin). It's wrong.

使用非常谨慎和节制,你可以使用:

With great care and moderation, you could use:

/* Right before `wgame` begins. */
while((c = getchar()) != '\n' && c != EOF)
    ;

不过,要知道它应谨慎使用,丢弃用户的输入是很危险的。

However, be aware it should be used sparingly, discarding user input is dangerous.

阅读Ç常见问题解答关于这个问题,和的解释约冲洗标准输入

Read this C FAQ on the subject, and an explanation about flushing stdin.

这篇关于获取(串#)函数跳过第一个获得请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆