循环问题 [英] loop question

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

问题描述

Hi Group,

我可以问以下问题吗?


for(i = 0;(c = getchar())!= EOF& & c!=''\ n''; ++ i)

line [i] = c;

我发现当c == ''\ n''是真的,循环是

终止,但我仍然增加。我的印象是

终止循环不应该增加i。

谢谢。

解决方案



morpheus写道:

嗨组,
我可以问以下问题吗?

(i = 0;(c = getchar())!= EOF&& c!=''\ n''; ++ i)
line [i] = c;

我发现当c ==''\ n''为真时,循环终止,但我仍然增加。我的印象是
终止循环不应该增加i。
谢谢




你可以发一个示例输入输出该计划以及

您的预期输出


ranjmis ha scritto:

morpheus写道:

嗨组,
请问以下问题?

for(i = 0;(c = getchar())!= EOF&& c!=''\ n''; ++ i)
line [i] = c;

我发现当c ==''\ n''为真时,循环终止,但我仍然是递增。我的印象是
终止循环不应该增加i。
谢谢



请你发一个程序的示例输入输出与
你的预期输出




而且,我想补充一下,请你发一个完整的代码*,而不仅仅是一个

片段。所以我们可以看到你的循环出了什么问题。


David


-

Linux注册用户#334216

获取FireFox! >> http://www.spreadfirefox.com/? q =关联公司& id = 48183& t = 1

员工>> http://www.debianizzati.org <<




ranjmis写道:

for(i = 0;(c = getchar( ))!= EOF&& c!=''\ n''; ++ i)
line [i] = c;


请你发一个样本输入 - 计划的输出以及
您的预期输出




我的实际代码是:


for(i = 0;(c = getchar())!= EOF&& c!=''\ n''; ++ i)

line [i] = c;


if(c ==''\ n''|| c == EOF)

{

++ i;

line [i] = ''\ n'';

++ i;

line [i] =''\ 0'';

}


测试输入; test




请注意,测试后面是4个空格和换行符号。


我期待线路包含''test\\\
\''',但它包含

test''something''\ n0。现在,我确实感谢第5行中的++ i引起了这种行为,但我不完全理解为什么第5行不会是必要的,因为我认为不需要



Hi Group,
May I ask the following?

for (i=0; (c=getchar()) != EOF && c != ''\n''; ++i)
line[i]=c;
What I am finding is that when c == ''\n'' is true, the loop is
terminated but i is still incremented. I was under the impression that
termination of the loop should not increment i.
Thank you.

解决方案


morpheus wrote:

Hi Group,
May I ask the following?

for (i=0; (c=getchar()) != EOF && c != ''\n''; ++i)
line[i]=c;
What I am finding is that when c == ''\n'' is true, the loop is
terminated but i is still incremented. I was under the impression that
termination of the loop should not increment i.
Thank you



can you please post a sample input-output of the program along with
your "expected" output


ranjmis ha scritto:

morpheus wrote:

Hi Group,
May I ask the following?

for (i=0; (c=getchar()) != EOF && c != ''\n''; ++i)
line[i]=c;
What I am finding is that when c == ''\n'' is true, the loop is
terminated but i is still incremented. I was under the impression that
termination of the loop should not increment i.
Thank you



can you please post a sample input-output of the program along with
your "expected" output



And, I''d add, could you please post a *complete code*, not just a
snippet. So we can see what''s wrong with your loop.

David

--
Linux Registered User #334216
Get FireFox! >> http://www.spreadfirefox.com/?q=affiliates&id=48183&t=1
Staff >> http://www.debianizzati.org <<



ranjmis wrote:

for (i=0; (c=getchar()) != EOF && c != ''\n''; ++i)
line[i]=c;


can you please post a sample input-output of the program along with
your "expected" output



My actual code is this:

for (i=0; (c=getchar()) != EOF && c != ''\n''; ++i)
line[i]=c;

if (c == ''\n'' || c == EOF )
{
++i;
line[i]=''\n'';
++i;
line[i]=''\0'';
}

Test input; "test
"

Note that test is followed by 4 blank spaces and a newline token.

I was expecting "line" to contain ''test\n\0'' but it contains
test''something''\n\0. Now, I do appreciate that ++i in line 5 is
causing this behaviour, but I do not fully appreciate why line 5 would
not be necessary, I guess.


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

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