做{...} while(); [英] do {...} while () ;

查看:78
本文介绍了做{...} while();的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白这个周期的原因:


做{

printf(&\\; \ nCome devono essere posizionati gli elementi? \ n");

printf(" * premere E,nel caso di posizionamento sul piano

E\ n);

printf("(ricezione onde orizzontali); \ n");

printf(" * premere H,nel caso di posizionamento sul piano

H \ n");

printf("(ricezione onde verticali)。\ n");

scanf("%c",& scelta_piano); < br(>
} while((scelta_piano!=''H'')&&(scelta_piano!=''h'')&&

(scelta_piano!= ''E'')&&(scelta_piano!=''e''));


写两次内容。所以输出是:


来自devono essere posizionati gli elementi?

* premere E,nel caso di posizionamento sul piano E

(ricezione onde orizzontali);

* premere H,nel caso di posizionamento sul piano H

(ricezione onde verticali)。


来自devono essere posizionati gli elementi?

* premere E,nel caso di posizionamento sul piano E

(ricezione onde orizzontali);

* premere H,nel caso di posizionamento sul piano H

(ricezione onde verticali)。


我希望有人可以帮助我。

再见Aleramo。

I don''t understand the reason cos this cycle:

do {
printf ("\nCome devono essere posizionati gli elementi?\n") ;
printf ("* premere E, nel caso di posizionamento sul piano
E\n") ;
printf ("(ricezione onde orizzontali);\n") ;
printf ("* premere H, nel caso di posizionamento sul piano
H\n") ;
printf ("(ricezione onde verticali).\n") ;
scanf ("%c", &scelta_piano) ;
} while ((scelta_piano != ''H'') && (scelta_piano != ''h'') &&
(scelta_piano != ''E'') && (scelta_piano != ''e'')) ;

write its content two time. So the output is:

Come devono essere posizionati gli elementi?
* premere E, nel caso di posizionamento sul piano E
(ricezione onde orizzontali);
* premere H, nel caso di posizionamento sul piano H
(ricezione onde verticali).

Come devono essere posizionati gli elementi?
* premere E, nel caso di posizionamento sul piano E
(ricezione onde orizzontali);
* premere H, nel caso di posizionamento sul piano H
(ricezione onde verticali).

I hope someone can help me.
Bye bye Aleramo.

推荐答案

Aleramo写道:
Aleramo wrote:
我不明白其中的原因这个周期:

做{
printf(&\\; \ nCome devono essere posizionati gli elementi?\ n");
printf(" * premere E,nel caso di posizionamento sul piano
E \ n");
printf("(ricezione onde orizzontali); \ n");
printf(" * premere H,nel caso di posizionamento sul piano
H\ n ;);
printf("(ricezione onde verticali)。\ n");
scanf("%c",& scelta_piano);
} while((scelta_piano! ='''H'')&& (scelta_piano!=''h'')&&
(scelta_piano!=''E'')&& (scelta_piano!=''e''));

写两次内容。 [...]
I don''t understand the reason cos this cycle:

do {
printf ("\nCome devono essere posizionati gli elementi?\n") ;
printf ("* premere E, nel caso di posizionamento sul piano
E\n") ;
printf ("(ricezione onde orizzontali);\n") ;
printf ("* premere H, nel caso di posizionamento sul piano
H\n") ;
printf ("(ricezione onde verticali).\n") ;
scanf ("%c", &scelta_piano) ;
} while ((scelta_piano != ''H'') && (scelta_piano != ''h'') &&
(scelta_piano != ''E'') && (scelta_piano != ''e'')) ;

write its content two time. [...]




可能是因为scanf()

读取的第一个字符不是H,h,E或e。您可以在scanf()之后添加

一个额外的printf()进行检查,以便进行调试

用途:


printf("输入''%c''(代码%d或0x%X)\ n",

scelta_piano,scelta_piano,scelta_piano);


那个幽灵角色来自哪里?可能是之前的scanf()

没有消耗的早期输入。

。例如,如果你的程序被执行


int numero;

...

printf("输入一个整数\ n");

scanf("%d"& numero);


....然后按4和2回复输入,

first scanf()将转换4和2,并将值

42存储在numero中。但是,该行还包含换行符

字符''\ n'',第一个scanf()不会吞下。

当第二个scanf()执行时,它找到换行符和

将它存储在scelta_piano中。你的程序看到''\ n''是

不是H,h,E,e之一,所以它重复。第二次通过

循环,scanf()找到你想要的角色,在scelta_piano中存储

,你的程序接受它。

scanf()不是一个非常好的交互式输入功能,因为它将输入视为一个长字符串。

通常,交互式输入应该是同步的通过一些

机制就像按下ENTER一样,这意味着它可以更自然地将交互式输入视为一系列完整的

行。解决这个问题的一种方法是使用fgets()

将完整的行读入字符数组,然后使用

sscanf() - 两个s' 'es - toread来自角色阵列。


-

Eric Sosman
es ***** @ acm-dot-org.inva lid



Probably because the first character read by scanf()
was not H, h, E, or e. You could check this by adding
an extra printf() right after the scanf(), for debugging
purposes:

printf ("Input is ''%c'' (code %d or 0x%X)\n",
scelta_piano, scelta_piano, scelta_piano);

Where did that phantom character come from? It was
probably from some earlier input that a previous scanf()
did not consume. For example, if your program executed

int numero;
...
printf ("Enter an integer\n");
scanf ("%d", &numero);

.... and you responded by pressing 4 and 2 and ENTER, the
first scanf() would convert the 4 and 2 and store the value
42 in numero. However, the line also contained a newline
character ''\n'', which the first scanf() would not swallow.
When the second scanf() executes, it finds the newline and
stores it in scelta_piano. Your program sees that ''\n'' is
not one of H,h,E,e, so it repeats. The second time through
the loop, scanf() finds the character you intended, stores
it in scelta_piano, and your program accepts it.

scanf() is not a very good function for interactive input,
because it treats the input as one long stream of characters.
Usually, interactive input should be "synchronized" by some
mechanism like pressing ENTER, and this means that it is more
natural to think of interactive input as a sequence of complete
lines. One way to deal with this problem is to use fgets()
to read a complete line into a character array, and then use
sscanf() -- two s''es -- to "read" from the character array.

--
Eric Sosman
es*****@acm-dot-org.invalid


Aleramo写道:
Aleramo wrote:
我不明白这个循环的原因:

做{
printf(&\\; \ nCome devono essere posizionati gli elementi?\ n");
printf(" * premere E,nel caso di posizionamento sul piano
E\ n);
printf("(ricezione onde orizzontali); \ n");
printf (" * premere H,nel caso di posizionamento sul piano
H\ n);
printf("(ricezione onde verticali)。\ nn quot;);
scanf( "%c"& scelta_piano);
} while((scelta_piano!=''H'')&&(scelta_piano!=''h'')&&
(scelta_piano!=''E'')&&(scelta_piano!=''e''));

两次写出它的内容。所以输出是:
I don''t understand the reason cos this cycle:

do {
printf ("\nCome devono essere posizionati gli elementi?\n") ;
printf ("* premere E, nel caso di posizionamento sul piano
E\n") ;
printf ("(ricezione onde orizzontali);\n") ;
printf ("* premere H, nel caso di posizionamento sul piano
H\n") ;
printf ("(ricezione onde verticali).\n") ;
scanf ("%c", &scelta_piano) ;
} while ((scelta_piano != ''H'') && (scelta_piano != ''h'') &&
(scelta_piano != ''E'') && (scelta_piano != ''e'')) ;

write its content two time. So the output is:




< snip>


将来,请提供*完整*可编译的示例

演示了您的问题,并告诉我们您正在使用的输入。

错误可能是你未发布的250000行代码中的任何一个。

然而,我心情很好,我猜你是有这个问题

在你没有发布的代码的某个地方
http://c-faq.com/stdio/scanfinterlace.html


一般情况下,我建议不要使用scanf。使用fgets(不是,*永远*

看起来更吸引人)然后使用选择的工具传递你读过的行

,sscanf,strtol,手写的传球手

或者其他什么。

-

Flash Gordon,生活在有趣的时代。

Web网站 - http://home.flash-gordon.me.uk/

comp.lang.c发布指南和介绍:
http://clc-wiki.net/wiki/Intro_to_clc



<snip>

In future, please provide a *complete* compilable example that
demonstrates your problem and also tell us what input you are using. The
bug could be anywhere in the 250000 lines of code you have not posted.
However, I''m in a good mood and I''ll guess that you have this problem
somewhere in the code you did not post
http://c-faq.com/stdio/scanfinterlace.html

In general I would recommend against using scanf. Use fgets (not, *ever*
the more appealing looking gets) and then pass the line you have read
using the tool of choice, be that sscanf, strtol, a hand written passer
or whatever.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc


Aleramo写道:
Aleramo wrote:

我不明白这个循环的原因:

做{
printf(&\\; \ nCome devono essere posizionati gli elementi?\ n");
printf (" * premere E,nel caso di posizionamento sul piano
E\ n);
printf("(ricezione onde orizzont)打印((" * premere H,nel caso di posizionamento sul piano
H\ n);
printf("(ricezione onde verticali) )。(。\ n");
scanf("%c"& scelta_piano);
} while((scelta_piano!=''H'')&& (scelta_piano!=''h'')&&
(scelta_piano!=''E'')&& (scelta_piano!=''e''));

写两次内容。所以输出是:

I don''t understand the reason cos this cycle:

do {
printf ("\nCome devono essere posizionati gli elementi?\n") ;
printf ("* premere E, nel caso di posizionamento sul piano
E\n") ;
printf ("(ricezione onde orizzontali);\n") ;
printf ("* premere H, nel caso di posizionamento sul piano
H\n") ;
printf ("(ricezione onde verticali).\n") ;
scanf ("%c", &scelta_piano) ;
} while ((scelta_piano != ''H'') && (scelta_piano != ''h'') &&
(scelta_piano != ''E'') && (scelta_piano != ''e'')) ;

write its content two time. So the output is:




我建议你以不同的方式思考它。我相信你想要提供一个提示,然后获得一个可能的回复

字符。因此,在函数中隔离此操作。


int promptandgetresponse(const char * prompt,

const char *可接受);


注意int的返回,以便可以检测到EOF。

假设stdout用于提示,并且

stdin用于输入。如果需要

,你可以添加参数来控制它。所以第一步是提示提示:


do {

if(prompt){

printf("%) s",prompt); fflush(stdout);

}

/ *现在得到回复* /

}而(/ *回复不满意* /) ;


注意fflush,它允许提示不以\ n结尾。提示上的

测试允许你完全避免提示,提供

NULL。


看这个你可能会看到一个可能的问题。用户如何知道为什么他的回答是不可接受的?所以你可能想修改系统的b / b $ b以便稍后给出反馈。这是一个

练习。


现在,我们如何得到回复?如果我们保留一些有用的

辅助例程,则无需使用高度不守规矩的scanf使事情变得复杂化




int skipblanks(void){

int ch;

while('''==(ch = getchar()))继续;

返回ch;

}


将吞噬领先的空白。我们还可以通过以下方式吞噬所有

未使用的线路部分:

+

int flushln(void){

int ch;

while((''\ n''!=(ch = getchar()))&&(EOF!= ch))继续;

返回ch;

}


请注意,如果遇到EOF,则两个例程都将退出并返回EOF

。这些例程非常有用,您应始终将它们用于交互式使用。它们更好

一般用于访问作为参数提供的任意文件。


现在让我们修饰一下advndndgetresponse例程来使用它们。


int promptandgetresponse(const char * prompt,

const char *可接受)

{

int ch;


do {

if(prompt){

printf("%s",prompt); fflush(stdout);

}

/ *现在得到回复* /

ch = skipblanks(); / *得到第一个非空格字符* /

if(''\ n''!= = ch)flushln(); / *设置再试一次* /

} while(/ *回复不满意* /);

返回ch;

}


这个还没有解决,回复并不令人满意然而,还有一些

其他小困难。您应该尝试以下条件:


strchr(可接受,ch)


来替换该评论。这将需要#include< strings.h> ;.


需要考虑的事项 :如果用户只输入空的话怎么办?

字符。如果在跳过空白的情况下遇到EOF而该怎么办?执行flushln时?您可以通过坚持使用''\ n'',

即< enter>来正确终止回复来解决大部分

这些问题。在大多数机器上。否则您将返回EOF

而不是尝试保留无效条目中的数据。毕竟,

一旦发出EOF信号,你就不能再做任何事了。


一旦你干净利落地运行,你就会解决很多问题

您未来的互动问题。请注意,例程

不需要很长很复杂,而应该很短,而且你应该能够在检查时轻易看到缺陷。


顺便说一句,请注意我提出的代码并没有被

语言困难所迷惑。提示是什么,它的语言,

没有任何影响。类似地,单个字符的翻译。

响应。


以上代码未经测试。所以要小心。


-

一些信息链接:

新闻:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart -questions.html
http://www.caliburn。 nl / topposting.html
http:// www .netmeister.org / news / learn2quote.html



I suggest you think about it differently. I believe you want to
present a prompt, and get one of a subset of possible response
characters. So isolate this operation in a function.

int promptandgetresponse(const char *prompt,
const char *acceptable);

Note the return of an int, so that an EOF can be detected. The
assumption is being made that stdout is used for prompting, and
stdin for input. You could add parameters to control this, if
desired. So the first step is to present the prompt:

do {
if (prompt) {
printf("%s", prompt); fflush(stdout);
}
/* Now get a reply */
} while ( /* reply is not satisfactory */ );

Note the fflush, which allows the prompt to not end with a \n. The
test on prompt allows you to avoid prompting entirely, by supplying
NULL.

Looking at this you may see a possible problem. How is the user to
know why his response is not acceptable? So you may want to modify
the system to give feedback on this later. This is left as an
exercise.

Now, how do we get a reply? There is no need to complicate things
with the highly unruly scanf if we keep a couple of useful
auxiliary routines around:

int skipblanks(void) {
int ch;
while ('' '' == (ch = getchar())) continue;
return ch;
}

which will gobble up leading blanks. We can also gobble up all
unused line portions with:
+
int flushln(void) {
int ch;
while ((''\n'' != (ch = getchar())) && (EOF != ch)) continue;
return ch;
}

Notice that both routines will exit, and return EOF, if an EOF is
encountered. These routines are so useful that you should always
have them available for interactive use. They are better
generalized to access arbitrary files, supplied as a parameter.

Now lets embellish the promptandgetresponse routine to use these.

int promptandgetresponse(const char *prompt,
const char *acceptable)
{
int ch;

do {
if (prompt) {
printf("%s", prompt); fflush(stdout);
}
/* Now get a reply */
ch = skipblanks(); /* get first non-blank char */
if (''\n'' != ch) flushln(); /* set up to try again */
} while ( /* reply is not satisfactory */ );
return ch;
}

This hasn''t resolved "reply is not satisfactory" yet, and has some
other minor difficulties. You should try the condition:

strchr(acceptable, ch)

to replace that comment. This will require #include <strings.h>.

Things to think about: What if the user enters only an empty
line? You could make this select a default, such as the first
character in acceptable. What if an EOF is encountered while
skipping blanks? while executing flushln? You can resolve most of
these by insisting that responses be properly terminated with ''\n'',
i.e. an <enter> on most machines. Otherwise you will return an EOF
and not try to preserve data from an invalid entry. After all,
once EOF has been signalled you can''t do anything more.

Once you get this running cleanly, you will have solved many of
your interactive problems for the future. Note that the routine(s)
need not be long and complex, rather they should be short and you
should be able to see flaws easily on inspection.

BTW, notice that the code I am proposing is not confused by
language difficulties. What the prompt is, and its language, have
no effect whatsoever. Similarly the translation of single char.
responses.

The above code is untested. So beware.

--
Some informative links:
news:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html


这篇关于做{...} while();的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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