scanf问题(“%c”,& answer); [英] Trouble with scanf("%c", &answer);

查看:84
本文介绍了scanf问题(“%c”,& answer);的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好


我遇到了麻烦scanf("%c",& answer);"在下面的第20行。当我在Windows 98SE上运行cygwin中的程序时,它完全跳过该行,

结束程序。 scanf是否存在%c问题?或者是我正在使用的操作

系统?


1 #include< stdio.h>

2

3 int main()

4 {

5浮动宽度,长度,面积;

6 char答案;

7

8做{

9 printf(\ nEnter矩形宽度);

10 scanf("%f"& width);

11 printf(\ nEnter矩形长度);

12 scanf("%f"& length);

13

14面积=长*宽;

15

16 printf(矩形的\ n区域是%.2f,区域);

17

18 printf(" \\\
Continue? - 输入Y代表是,N代表否;

19

20 scanf("%c"& answer);

21

22} while(回答==''Y'');

23

24返回0;

25}

26


谢谢


彼得山
** ** @ petermount.au.com

Hello

I''m having trouble with " scanf("%c", &answer);" on line 20 below. When I
run the program in cygwin on Windows 98SE it skips that line completely and
ends the program. Does scanf have problems with "%c" or is it the operating
system I''m using?

1 #include <stdio.h>
2
3 int main()
4 {
5 float width, length, area;
6 char answer;
7
8 do{
9 printf("\nEnter width of the rectangle ");
10 scanf("%f", &width);
11 printf("\nEnter length of the rectangle ");
12 scanf("%f", &length);
13
14 area = length * width;
15
16 printf("\nArea of the rectangle is %.2f", area);
17
18 printf("\nContinue? - Enter Y for yes, N for no ");
19
20 scanf("%c", &answer);
21
22 }while(answer == ''Y'');
23
24 return 0;
25 }
26

Thanks

Peter Mount
in**@petermount.au.com

推荐答案



Peter Mount <在** @ petermount.au.com>在消息中写道

新闻:40 ********** @ news.iprimus.com.au ...

"Peter Mount" <in**@petermount.au.com> wrote in message
news:40**********@news.iprimus.com.au...
你好
并结束该程序。 scanf是否存在%c问题?还是我正在使用的
操作系统?

1 #include< stdio.h>
2
3 int main()
4 {* 5浮动宽度,长度,面积;
6个字母的答案;
7
8做{
9 printf(" \\\
Enter width of the矩形");
10 scanf("%f",& width);
11 printf(\ nEnter矩形长度);
12 scanf( %f,& length);
13
14 area = length * width;
15 printf(矩形的\ n区域为%。 2f,区域;
17
18 printf(\ n继续? - 输入Y表示是,N表示否);
19
20 scanf(" ;%c,& answer);
21
22} while(回答==''Y'');
23
24返回0;
25}
感谢

Peter Mount
in * *@petermount.au.com




查看常见问题解答 - 很好地解答了这个问题。

提示:你之前的scanf()还没有清除输入缓冲区。

Allan



Have a look at the FAQ - this is answered there nicely.
HINT: Your previous scanf() hasnt cleared the input buffer.
Allan


你好


我通过将第20行更改为阅读:


scanf("%s",& answer);


这是因为我使用的是Windows 98SE吗?我原以为scanf("%c,

& answer);"本来是正确的回答被宣布为char

变量。


此外,FAQ在哪里?我正在为我的新闻组使用Outlook Express,所以我

没有看到常见问题,我想阅读常见问题,如果可以的话


谢谢


Peter Mount
in**@petermount.au.com


" Allan Bruce" <人***** @ TAKEAWAYf2s.com>在消息中写道

news:ca ********** @ news.freedom2surf.net ...
Hello

I got it to work by changing line 20 to read:

scanf("%s", &answer);

Is this because I''m using Windows 98SE? I would have thought "scanf("%c",
&answer);" would have been correct as "answer" was declared as a char
variable.

Also, where is the FAQ? I''m using Outlook Express for my newsgroups so I
don''t see the FAQ and I would like to read the FAQ if I could

Thanks

Peter Mount
in**@petermount.au.com

"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message
news:ca**********@news.freedom2surf.net...

Peter Mount <在** @ petermount.au.com>在消息中写道
新闻:40 ********** @ news.iprimus.com.au ...

"Peter Mount" <in**@petermount.au.com> wrote in message
news:40**********@news.iprimus.com.au...
你好

我' 我遇到了麻烦 scanf("%c",& answer);"在下面的第20行。当
我在Windows 98SE上运行cygwin中的程序时,它完全跳过该行
Hello

I''m having trouble with " scanf("%c", &answer);" on line 20 below. When I run the program in cygwin on Windows 98SE it skips that line completely


并且

结束程序。 scanf是否存在%c问题?或者是
ends the program. Does scanf have problems with "%c" or is it the


操作

我正在使用的系统?

1 #include< stdio.h>
2
3 int main()
4 {
5个浮动宽度,长度,面积;
6个字母的答案;
7
8做{
9 printf(\ nEnter矩形的宽度);
10 scanf(%f,& width);
11 printf(" \\\
Enter长度为矩形);
12 scanf(%f,& length);
13
14 area = length * width;
15
16 printf (矩形的\ n区域是%.2f,区域);
17
18 printf(" \ nContinue? - 输入Y代表是,N代表否);
19
20 scanf("%c"& answer);
21
22} while(answer ==''Y'');
23
24返回0;
25}
26谢谢

彼得山
** ** @ petermount.au.com
system I''m using?

1 #include <stdio.h>
2
3 int main()
4 {
5 float width, length, area;
6 char answer;
7
8 do{
9 printf("\nEnter width of the rectangle ");
10 scanf("%f", &width);
11 printf("\nEnter length of the rectangle ");
12 scanf("%f", &length);
13
14 area = length * width;
15
16 printf("\nArea of the rectangle is %.2f", area);
17
18 printf("\nContinue? - Enter Y for yes, N for no ");
19
20 scanf("%c", &answer);
21
22 }while(answer == ''Y'');
23
24 return 0;
25 }
26

Thanks

Peter Mount
in**@petermount.au.com



查看常见问题解答 - 这很好地解答了。<提示:你之前的scanf()还没有清除输入缓冲区。
Allan



Have a look at the FAQ - this is answered there nicely.
HINT: Your previous scanf() hasnt cleared the input buffer.
Allan



Peter Mount写道:
Peter Mount wrote:

我遇到了 scanf("%c",& answer);"在下面的第20行。
当我在Windows 98SE上运行cygwin中的程序时,它会完全跳过该行,并结束该程序。 scanf是否存在问题
%c还是我正在使用的操作系统?

1 #include< stdio.h>
2
3 int main()
4 {
5浮动宽度,长度,面积;
6 char答案;
7
8做{
9 printf(\ nEnter Rect of the rectangle) ;<> 10 scanf("%f"& width);
11 printf(\ nEnnter矩形长度);
12 scanf("%f" ;,& length);
13
14 area = length * width;
15
16 printf(矩形的\ n区域为%.2f,区域);
17
18 printf(\ nContinue? - 输入Y表示是,N表示否);
19
20 scanf("%c" ,&回答);


scanf("%c",& answer); 21
22} while(回答==''Y'');
23
24返回0;
25}
26

I''m having trouble with " scanf("%c", &answer);" on line 20 below.
When I run the program in cygwin on Windows 98SE it skips that
line completely and ends the program. Does scanf have problems
with "%c" or is it the operating system I''m using?

1 #include <stdio.h>
2
3 int main()
4 {
5 float width, length, area;
6 char answer;
7
8 do {
9 printf("\nEnter width of the rectangle ");
10 scanf("%f", &width);
11 printf("\nEnter length of the rectangle ");
12 scanf("%f", &length);
13
14 area = length * width;
15
16 printf("\nArea of the rectangle is %.2f", area);
17
18 printf("\nContinue? - Enter Y for yes, N for no ");
19
20 scanf("%c", &answer);
scanf(" %c", &answer); 21
22 } while(answer == ''Y'');
23
24 return 0;
25 }
26



但是在交互式文件中使用scanf时会遇到问题。

您还犯了致命错误,即没有检查

scanf返回值。 printf()s之后应该是

fflush(stdout);


你可以使用几个包装器处理大部分这些东西

功能:


#include< stdio.h>


无效提示(char * line)

{

printf(line);

fflush(stdout);

}


void getfloat(float * fv,char * promptln)

{

do {

prompt(promptln);

} while(1!= scanf("%f",fv))''

}


int getonechar(char * promptln)

{

int ch;


if(1!= scanf("%c",ch))return 0;

返回(unsigned char)ch;

}


/ *之后你的例程减少到:* /


int main()

{

浮动宽度,长度,面积;


do {

getfloat(& width," \ nEnter Rect of the rectangle");

getfloat(& length," \\ n输入矩形的长度);

area = length * width;

printf(矩形的\ n区域为%.2f,区域) ;

} while(''Y''== toupper(getonechar(

" \ nContinue? - 输入Y代表是,N代表否)));

返回0;

}


注意子程序如何简化和澄清。然而,由于

使用scanf,因此仍然存在各种令人讨厌的交互式使用问题。尝试使用2 3 Y

(不带引号)回复第一个提示。


-

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

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

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



However you will have problems using scanf in interactive files.
You have also committed the fatal mistake of not checking the
scanf return value. The printf()s should be followed by
fflush(stdout);

You can handle most of these things with a couple of wrapper
functions:

#include <stdio.h>

void prompt(char *line)
{
printf(line);
fflush(stdout);
}

void getfloat(float *fv, char *promptln)
{
do {
prompt(promptln);
} while (1 != scanf("%f", fv))''
}

int getonechar(char *promptln)
{
int ch;

if (1 != scanf(" %c", ch)) return 0;
return (unsigned char) ch;
}

/* after which your routine reduces to: */

int main()
{
float width, length, area;

do {
getfloat(&width, "\nEnter width of the rectangle ");
getfloat(&length, "\nEnter length of the rectangle ");
area = length * width;
printf("\nArea of the rectangle is %.2f", area);
} while(''Y'' == toupper(getonechar(
"\nContinue? - Enter Y for yes, N for no ")));
return 0;
}

Notice how subroutines simplify and clarify. However that still
has all sorts of nasty problems for interactive use, because of
the use of scanf. Try responding to the first prompt with "2 3 Y"
(without the quotes) for example.

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


这篇关于scanf问题(“%c”,&amp; answer);的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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