一些带有“int”的Newb问题,请帮忙。 [英] Some Newb Problem with "int", please help.

查看:89
本文介绍了一些带有“int”的Newb问题,请帮忙。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

教授给我们的问题是:


编写一个读取两个整数值的程序。如果第一个比第二个更少,则打印消息up。如果第二个小于第一个,则打印消息向下。如果数字相等,则打印

消息等于如果读取数据时出错,请打印包含单词Error的

消息。并执行退出(0);

这就是我写的:


main()

{

int x;

int y;


printf("输入第一个整​​数:");

scanf("%d",& x);


printf("输入第二个整数:");

scanf(" ;%d",& y);


if(x> y)

{

printf(" Up);

}

else

{

if(x< y)

{

printf(" Down");

}

其他

{

if(x = y)

{

printf(" Equal);

}

else

{

printf(" Error);

getch();

退出(0);

}

}

}

}


最初工作正常,但每当我输入无效字符,例如

!,A,%,:等时,它永远不会要求我输入第二个整数并跳到

printf(向下);

任何人都可以向我解释为什么会发生这种情况吗?

The problem professor gave us is:

Write a program which reads two integer values. If the first is less
than the second, print the message "up". If the second is less than
the first, print the message "down" If the numbers are equal, print
the message "equal" If there is an error reading the data, print a
message containing the word "Error" and perform exit( 0 );
And this is what I wrote:

main()
{
int x;
int y;

printf("Enter the first integer: ");
scanf("%d", &x);

printf("Enter the second integer: ");
scanf("%d", &y);

if (x > y)
{
printf("Up");
}
else
{
if (x < y)
{
printf("Down");
}
else
{
if (x = y)
{
printf("Equal");
}
else
{
printf("Error");
getch();
exit (0);
}
}
}
}

Works fine at first, but whenever I enter invalid characters such as
!, A, %, :, etc. It never asks me for the second integer and skip to
printf("Down");
Can anyone be kind enough to explain to me why that happened?

推荐答案

2004-09-20,Apotheosis< he ************* @ yahoo.com>写道:
On 2004-09-20, Apotheosis <he*************@yahoo.com> wrote:

[代码使用scanf剪切]

一开始工作正常,但每当我输入无效字符,如
!,A,% ,:等等它永远不会要求我输入第二个整数并跳到
printf(向下);
任何人都可以向我解释为什么会发生这种情况吗?
[ code using scanf snipped ]

Works fine at first, but whenever I enter invalid characters such as
!, A, %, :, etc. It never asks me for the second integer and skip to
printf("Down");
Can anyone be kind enough to explain to me why that happened?




不要使用scanf。阅读C-faq

中问题12.20的答案,找出原因。
http://www.eskimo.com/~scs/C-faq/top.html

- James


老兄,你正在阅读C-faq!



Don''t use scanf. Read the answer to question 12.20 in the C-faq
to find out why.

http://www.eskimo.com/~scs/C-faq/top.html

-- James

Dude, you''re reading the C-faq!


Apotheosis写道:
Apotheosis wrote:

教授给我们的问题是:

编写一个读取两个整数值的程序。如果第一个小于第二个,则打印消息up。如果第二个小于第一个,则打印消息向下。如果数字相等,则打印消息相等。如果读取数据时出错,请打印包含错误一词的消息。然后执行exit(0);

这就是我写的:

main()
{
int x;
int y;

printf("输入第一个整​​数:");
scanf("%d",& x);

printf( 输入第二个整数:;;
scanf("%d",& y);

.... snip ...
工作正常起初,但每当我输入无效字符,如
!,A,%,:等。它从未要求我输入第二个整数并跳到
printf(向下);

The problem professor gave us is:

Write a program which reads two integer values. If the first is less
than the second, print the message "up". If the second is less than
the first, print the message "down" If the numbers are equal, print
the message "equal" If there is an error reading the data, print a
message containing the word "Error" and perform exit( 0 );

And this is what I wrote:

main()
{
int x;
int y;

printf("Enter the first integer: ");
scanf("%d", &x);

printf("Enter the second integer: ");
scanf("%d", &y);
.... snip ...
Works fine at first, but whenever I enter invalid characters such as
!, A, %, :, etc. It never asks me for the second integer and skip to
printf("Down");
Can anyone be kind enough to explain to me why that happened?




基本上你没有检测到由

引起的错误scanf调用。这些都返回一个值,所以他们

应该是这样的形式:


if(1!= scanf("%d",&) ; x))erroraction();

else {

....

}


切勿在未检查返回值的情况下使用scanf。另外,main

返回一个int。这样说的。您应该使用:


int main(无效)

{

....

返回0;

}


-

Chuck F(cb ******** @ yahoo。 com)(cb********@worldnet.att.net)

可用于咨询/临时嵌入式和系统。

< http:// cbfalconer.home.att.net>使用worldnet地址!



Basically you have failed to detect the errors resulting from the
scanf calls. Those are both returning a single value, so they
should be of the form:

if (1 != scanf("%d", &x)) erroraction();
else {
....
}

Never use scanf without checking the return value. Also, main
returns an int. Say so. You should be using:

int main(void)
{
....
return 0;
}

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


Apotheosis< he ************* @ yahoo.com>潦草地写道:
Apotheosis <he*************@yahoo.com> scribbled the following:
教授给我们的问题是:
编写一个读取两个整数值的程序。如果第一个小于第二个,则打印消息up。如果第二个小于第一个,则打印消息向下。如果数字相等,则打印消息相等。如果读取数据时出错,请打印包含错误一词的消息。并执行exit(0);
这就是我写的:


main()
{
int x;
int y;
printf("输入第一个整​​数:");
scanf("%d",& x);
printf("输入第二个整数:");
scanf("%d",& y);
if(x> y)
{
printf(" Up");
}

{
if(x < y)
{
printf(" Down");
}

{
if(x = y)


其他人都未能发现这一点。你想要==这里,而不是=。 =第一个

将y'的值赋给x,然后测试x'的值是否为其他任何值,而不是
。这意味着如果执行到目前为止,输入的任何值

到y,只要它不是0,将打印Equal。无论

x'的价值。因为你已经测试了x> y和x < y,这将是

,否则无关紧要,但如果你输入0来支付
x和y,它就会破裂。在这种情况下,你的程序根本不会打印任何内容。

{
printf(" Equal);
}

{
printf(" Error");
getch();
退出(0);
}
}
}
}



最初工作正常,但每当我输入无效字符,例如
!,A,%,:等时,它从未要求我输入第二个整数并跳到
printf(向下);
任何人都可以向我解释为什么会发生这种情况?
The problem professor gave us is: Write a program which reads two integer values. If the first is less
than the second, print the message "up". If the second is less than
the first, print the message "down" If the numbers are equal, print
the message "equal" If there is an error reading the data, print a
message containing the word "Error" and perform exit( 0 ); And this is what I wrote:
main()
{
int x;
int y; printf("Enter the first integer: ");
scanf("%d", &x); printf("Enter the second integer: ");
scanf("%d", &y); if (x > y)
{
printf("Up");
}
else
{
if (x < y)
{
printf("Down");
}
else
{
if (x = y)
Everyone other failed to spot this. You want == here, not =. = first
assigns y''s value to x and then tests if x''s value is anything other
than 0. This means that if execution gets this far, any value entered
to y at all, as far as it isn''t 0, will print "Equal" regardless of
x''s value. Because you already tested for x > y and x < y, this would
otherwise not matter, but it''s going to break if you type 0 for both
x and y. In this case your program won''t print anything at all.
{
printf("Equal");
}
else
{
printf("Error");
getch();
exit (0);
}
}
}
}

Works fine at first, but whenever I enter invalid characters such as
!, A, %, :, etc. It never asks me for the second integer and skip to
printf("Down");
Can anyone be kind enough to explain to me why that happened?




-

/ - Joona Palaste(pa*****@cc.helsinki.fi)-------------芬兰-------- \\ \\

\ - http://www.helsinki。 fi / ~palaste ---------------------规则! -------- /

我希望我们认识的人会死,所以我们可以给他们留下鲜花。

- 一个6岁的孩子女孩,在墓地里看到鲜花



--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"I wish someone we knew would die so we could leave them flowers."
- A 6-year-old girl, upon seeing flowers in a cemetery


这篇关于一些带有“int”的Newb问题,请帮忙。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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