我正在尝试编写一个显示菜单的小程序,并且 用户输入一个值(int)。我只使用getchar和printf制作 简单的东西。 我的菜单打印部分很好,但我得到了错误 当您输入带有n位数的 值时,整个菜单将打印n + 1次。我问了一下,其他一些人说,这是因为getchar是缓冲的,而且只会打印传递的每个字符 。我一直坚持如何制作它以便菜单只打印一次b $ b,然后请求用户输入。 这是怎么回事现在设置: 打印菜单 getchar while(getchar变量不是EOF) { 打印菜单 (菜单的条件......) getchar } 谁能给我任何关于如何解决这个问题的想法?对不起,如果这是一个愚蠢的问题。提前致谢! Hi everyone, I''m kind of new to C programming, so I thought that anewsgroup would be the best place to ask a question about this. I''m trying to write a small program that displays a menu and has theuser input a value(int). I''m only using getchar and printf to makethings simple. I have the menu printing part down pretty well, but I''m getting an errorwhere the entire menu will print n+1 number of times when you input avalue with n-digits. I asked around a little, and some others said thatit was because getchar is buffered, and will just print for every charthat is passed. I''m stuck on how to make it so that the menu will onlyprint once, then ask the use for input. This is how it is set up right now: prints menugetcharwhile (getchar variable is not EOF){prints menu(conditionals for menu...)getchar} Can anyone give me any ideas on how to fix this? Sorry if this is astupid question. Thanks in advance!推荐答案 " sk" < SK ****** @ purdue.edu>在消息中写道 news:7a5Nf.794387 "sk" <sk******@purdue.edu> wrote in messagenews:7a5Nf.794387 x96.655350@attbi_s72 ... x96.655350@attbi_s72...大家好,我是C编程的新手,所以我认为新闻组是提出这个问题的最好的地方。 我正在尝试写一个小程序,显示一个菜单并且用户输入一个值(int)。我只使用getchar和printf来简化。 我的菜单打印部分很好,但是我收到了一个错误当您输入带有n位数的值时,整个菜单将打印n + 1次。我问了一下,其他一些人说这是因为getchar是缓冲的,只会打印传递的每个字符。我坚持如何制作它以便菜单只打印一次,然后询问使用输入。 这就是它现在的设置: getchar while(getchar变量不是EOF) {打印菜单(菜单的条件......) /> getchar } 任何人都可以给我任何关于如何解决这个问题的想法?对不起,如果这是一个愚蠢的问题。提前致谢! Hi everyone, I''m kind of new to C programming, so I thought that a newsgroup would be the best place to ask a question about this. I''m trying to write a small program that displays a menu and has the user input a value(int). I''m only using getchar and printf to make things simple. I have the menu printing part down pretty well, but I''m getting an error where the entire menu will print n+1 number of times when you input a value with n-digits. I asked around a little, and some others said that it was because getchar is buffered, and will just print for every char that is passed. I''m stuck on how to make it so that the menu will only print once, then ask the use for input. This is how it is set up right now: prints menu getchar while (getchar variable is not EOF) { prints menu (conditionals for menu...) getchar } Can anyone give me any ideas on how to fix this? Sorry if this is a stupid question. Thanks in advance! 您可以考虑使用do-while循环,因为它可能更适合您的需求。 就实施而言您可以尝试阅读用户输入的第一个 字符并丢弃其余的输入行。一行的结束 用getchar()返回''\ n''来表示。在任何情况下你应该 总是注意你已经拥有的EOF。不要忘记在int中存储 getchar()返回值。请随意在您的 尝试发布所需行为。 You may consider using a do-while loop since it may suit your needs better.As far as the implementation is concerned, you can try reading the firstcharacter of the user input and discard the rest of the input line. The endof a line is signaled with getchar() returning ''\n''. In any case you shouldalways look out for EOF as you already have. Do not forget to store thegetchar() return in an int. Feel free to post your code here in yourattempts towards the desired behaviour. stathis gotsis写道:stathis gotsis wrote: " SK" < SK ****** @ purdue.edu>在消息中写道新闻:7a5Nf.794387 "sk" <sk******@purdue.edu> wrote in message news:7a5Nf.794387 这篇关于菜单循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-16 18:54