难题! [英] Puzzle!

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

问题描述

前几天,我写了一个这样的迷你程序:

#include< stdio.h>


int main()

{

char c;

int n;


scanf("%d",& n);

c = getchar();


返回0;

}


我想分配n和c特定值,例如,当我输入5然后输入时,给n和c

值为5和''x'',程序

结束。我知道当我按下Enter时,我将''\ n''分配给变量c,

但是我怎么能避免这个? br />

Some days ago, I written a mini program like this:
#include <stdio.h>

int main()
{
char c;
int n;

scanf("%d",&n);
c = getchar();

return 0;
}

I want to assign n and c specific values,for example,give n and c
values of 5 and ''x'' in fact when I input 5 then "enter",the program
is finish.I know when I press "Enter", I assigned ''\n'' to variable c,
But how can I avoid this?

推荐答案

On 6 2,1 15,Tak< kakat ... @ gmail.comwrote:
On 6 2 , 1 15 , Tak <kakat...@gmail.comwrote:

前几天,我写了一个这样的迷你程序:

#include< stdio.h>


int main()

{

char c;
Some days ago, I written a mini program like this:
#include <stdio.h>

int main()
{
char c;



错误:int c不是char c

a mistake: "int c" not "char c"


int n;


scanf("%d",& n);

c = getchar();


返回0;


}
int n;

scanf("%d",&n);
c = getchar();

return 0;

}



在文章< 11 ********************* @ i13g2000prf.googlegroups中。 com>,

Tak< ka ****** @ gmail.comwrote:
In article <11*********************@i13g2000prf.googlegroups. com>,
Tak <ka******@gmail.comwrote:

>前几天,我写的像这样的迷你程序:
#include< stdio.h>
>Some days ago, I written a mini program like this:
#include <stdio.h>


> int main()
{
char c;
int n;
scanf ("%d"& n);
c = getchar();
返回0;
}
>int main()
{
char c;
int n;
scanf("%d",&n);
c = getchar();
return 0;
}


>我想分配n和c特定值,例如,当我输入5然后输入时,给出n和c
值5和''x'',程序
结束。我知道当我按下Enter时,我将''\ n''分配给变量c,
但是我怎么能避免这种情况?
>I want to assign n and c specific values,for example,give n and c
values of 5 and ''x'' in fact when I input 5 then "enter",the program
is finish.I know when I press "Enter", I assigned ''\n'' to variable c,
But how can I avoid this?



while((c = getchar())!= EOF&& c ==''\ n''){/ *空循环体* /}


之后,c将是EOF或\ n以外的字符。

这考虑到用户可能会多次按回车

在输入所需的角色之前。


但为了使这项工作,你将不得不解决另一张海报的问题

指出:c应该是int类型,而不是char类型。


-

原型是其克隆的超类型。 - maplesoft

while ( (c = getchar()) != EOF && c == ''\n'' ) { /* empty loop body */ }

Afterwards, c will either be EOF or a character other than \n .
This takes into account that the user might press return several times
before entering the desired character.

But to make this work, you will have to fix the problem the other poster
pointed out: that c should be of type int, not of type char.

--
Prototypes are supertypes of their clones. -- maplesoft


Tak< ka ****** @ gmail.comwrites:
Tak <ka******@gmail.comwrites:

Some几天前,我写了一个这样的迷你程序:

#include< stdio.h>


int main()

{

char c;

int n;


scanf("%d",& n);

c = getchar();


返回0;

}


我想要分配n和c特定值,例如,当我输入5然后输入时,给出n和c

值为5和''x'',程序

结束。我知道当我按下Enter时,我将''\ n''分配给变量c,

但是我怎么能避免这种情况?
Some days ago, I written a mini program like this:
#include <stdio.h>

int main()
{
char c;
int n;

scanf("%d",&n);
c = getchar();

return 0;
}

I want to assign n and c specific values,for example,give n and c
values of 5 and ''x'' in fact when I input 5 then "enter",the program
is finish.I know when I press "Enter", I assigned ''\n'' to variable c,
But how can I avoid this?



(你已经在后续文章中提到c需要是一个int。)


如果你键入" 5X"然后< enter>,程序会将5分配给n和

''x''到c。


在一个真实的程序中,那就是是一种非常不友好的输入格式。如果

你必须使用scanf,你应该(a)检查它的返回值,并且(b)

知道输入流将在调用后所处的状态,并据此行动
;例如,你可能想要读取并忽略所有

字符,包括换行符(并且不要忘记检查

的EOF)。


处理输入的更好方法是使用

fgets()一次读取一行,然后在输入行后再分析输入行。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *< http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长

(You''ve already mentioned in a followup that c needs to be an int.)

If you type "5x" and then <enter>, the program will assign 5 to n and
''x'' to c.

In a real program, that would be a very unfriendly input format. If
you must use scanf, you should (a) check its return value, and (b) be
aware of the state the input stream will be in after the call, and act
accordingly; for example, you might want to read and ignore all
characters up to and including a newline (and don''t forget to check
for EOF).

A better way to handle input is to read a line at a time using
fgets(), and then analyze the input line once you have it.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


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

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