难题!

编程入门 行业动态 更新时间:2024-10-07 02:24:28
本文介绍了难题!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

前几天,我写了一个这样的迷你程序: #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 ... @ gmailwrote: On 6 2 , 1 15 , Tak <kakat...@gmailwrote: 前几天,我写了一个这样的迷你程序: #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 ****** @ gmailwrote: In article <11*********************@i13g2000prf.googlegroups. com>, Tak <ka******@gmailwrote: >前几天,我写的像这样的迷你程序: #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 cvalues of 5 and ''x'' in fact when I input 5 then "enter",the programis 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 ****** @ gmailwrites: Tak <ka******@gmailwrites: 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 < www.ghoti/~kst> 圣地亚哥超级计算机中心< *< 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 <www.ghoti/~kst> San Diego Supercomputer Center <* <users.sdsc.edu/~kst> "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"

更多推荐

难题!

本文发布于:2023-11-29 15:11:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646754.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:难题

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!