读取循环内的键 [英] Reading a key inside a loop
问题描述
当我在循环中使用getchar()读取一个键时,程序停止并且
等待按下一个键。我实际上希望程序继续执行它直到按下一个键才执行
。看看这个样本:
------
而(1 = 1)
{
printf(" oi);
ch = getchar();
如果(ch ==''q'')中断;
}
我希望程序永远打印''hi''(类似
" hihihihihihihihihihihihihi,直到''q''被按下。有没有简单的
方式来做这个?
(这个程序实际上停止打印''hi''并等待一个键
要被按下了。我不想要这个!)
对不好的英语很抱歉
When i read a key using getchar() inside a loop, the program stops and
wait for a key to be pressed. I actually want the program to continue
its execution until a key is pressed. Look at this sample:
------
while(1=1)
{
printf("oi");
ch=getchar();
if (ch==''q'') break;
}
I want the program to print ''hi'' forever(something like
"hihihihihihihihihihihihihi" until ''q'' is pressed. Is there an easy
way to do this?
(this program actually stop the printing of ''hi'' and waits for a key
to be pressed. I dont want this!)
Sorry about the bad english
推荐答案
On 3月29日,16:55,hstagni< sta ... @ gmail.comwrote:
On 29 Mar, 16:55, "hstagni" <sta...@gmail.comwrote:
当我在循环中使用getchar()读取密钥时,程序停止并且
等待按下一个键。我实际上希望程序继续执行,直到按下一个键为
.
When i read a key using getchar() inside a loop, the program stops and
wait for a key to be pressed. I actually want the program to continue
its execution until a key is pressed.
FAQ 19.1 - 见
FAQ 19.1 - see http://c-faq.com/ or more particularly http://c-faq.com/osdep/cbreak.html
看看这个样本:
------
而(1 = 1)
Look at this sample:
------
while(1=1)
for(;;)或while(1)有什么问题?
What''s wrong with for(;;) or while(1) ?
{
printf(" oi);
ch = getchar();
if(ch ==''q'')break;
}
我希望程序永远打印''hi''
{
printf("oi");
ch=getchar();
if (ch==''q'') break;
}
I want the program to print ''hi'' forever
好吧它赢了'当你编码oi时,你会这样做...
Well it won''t do so when you''ve coded "oi"...
" hstagni" < st **** @ gmail.comha scritto nel messaggio
news:11 ********************* @ n76g2000hsh.googlegro ups.com ...
"hstagni" <st****@gmail.comha scritto nel messaggio
news:11*********************@n76g2000hsh.googlegro ups.com...
当我在循环中使用getchar()读取密钥时,程序停止并且
等待按下键。我实际上希望程序继续执行它直到按下一个键才执行
。看看这个样本:
------
而(1 = 1)
{
printf(" oi);
ch = getchar();
如果(ch ==''q'')中断;
}
我希望程序永远打印''hi''(类似
" hihihihihihihihihihihihihi,直到''q''被按下。有没有简单的
方式来做这个?
(这个程序实际上停止打印''hi''并等待一个键
要被按下了。我不想要这个!)
When i read a key using getchar() inside a loop, the program stops and
wait for a key to be pressed. I actually want the program to continue
its execution until a key is pressed. Look at this sample:
------
while(1=1)
{
printf("oi");
ch=getchar();
if (ch==''q'') break;
}
I want the program to print ''hi'' forever(something like
"hihihihihihihihihihihihihi" until ''q'' is pressed. Is there an easy
way to do this?
(this program actually stop the printing of ''hi'' and waits for a key
to be pressed. I dont want this!)
你为什么要这样?
无论如何,没有便携的做法那取决于你正在做什么系统
。
Why do you want that?
Anyway, there is no portable way of doing that. It depends on what system
you''re working on.
http://c-faq.com/osdep/cbreak.html
" hstagni"< st * ***@gmail.comha scritto nel messaggio
news:11 ********************* @ n76g2000hsh.googlegro ups.co m ...
"hstagni" <st****@gmail.comha scritto nel messaggio
news:11*********************@n76g2000hsh.googlegro ups.com...
while(1 = 1)
while(1=1)
这会尝试分配1到1。但你不能指定一个十进制常数。
你的意思是(1 == 1),或者,如mark_bluemel指出的那样,而(1)。
This tries to assign 1 to 1. But you can''t assign to a decimal constant.
You meant while (1==1), or, as mark_bluemel pointed out, while (1).
这篇关于读取循环内的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!