菜单循环 [英] Menu looping

查看:62
本文介绍了菜单循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我是C编程的新手,所以我认为

新闻组是提出这个问题的最佳地点。

我正在尝试编写一个显示菜单的小程序,并且

用户输入一个值(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 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!

推荐答案

" sk" < SK ****** @ purdue.edu>在消息中写道

news:7a5Nf.794387
"sk" <sk******@purdue.edu> wrote in message
news: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 first
character of the user input and discard the rest of the input line. The end
of a line is signaled with getchar() returning ''\n''. In any case you should
always look out for EOF as you already have. Do not forget to store the
getchar() return in an int. Feel free to post your code here in your
attempts 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


这篇关于菜单循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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