程序始终输入switch语句的默认情况
程序片段是这样的:
int choice; / *用户输入命令* /
..
..
..
printf(" Enter命令:");
fflush(stdin);
choice = getc(stdin);
printf(" \\\
" ;);
..
..
..
while(choice!=''Q' ')
{
开关(选择)
{
/ *运动选项* /
case''n'':
dx = 0;
dy = -1;
action = 1;
休息;
案例'''':
..
..
..
..
..
默认:
printf("你做不到! \ n");
action = 0;
休息;
}
..
..
..
printf("输入命令:");
fflush(stdin);
choice = getc(stdin);
printf(" \ n");
} / *< - while循环的结束* /
我正在观察的问题是Switch命令的默认部分
一直在进入(第一次除外)。
我是猜测选择变量正在获得另一个值,因为
导致它进入默认值;但是使用
getchar,getc(stdin),fgetc(stdin)和刷新stdin并没有解决问题。
---
完整来源(有错误)在这里:
http://geocities.com/uthcode2/wumpus.c
''追捕Wumpus''游戏。
你编译和执行时可以看到问题(在Linux上,gcc)
问候,
Senthil
The Program Fragment is this:
int choice; /* Users Input Command */
..
..
..
printf("Enter a command: ");
fflush(stdin);
choice = getc(stdin);
printf("\n");
..
..
..
while(choice!=''Q'')
{
switch(choice)
{
/* Movement options */
case ''n'':
dx = 0;
dy = -1;
action = 1;
break;
case ''s'':
..
..
..
..
..
default:
printf("You cannot do that!\n");
action = 0;
break;
}
..
..
..
printf("Enter a Command :");
fflush(stdin);
choice = getc(stdin);
printf("\n");
} /* <- End Brace of while loop */
The Problem I am observing is the Default Section of the Switch command
is being entering all the times (except for the first time).
I am guessing that the choice variable is getting someother value which
is causing it to enter the default; but using
getchar,getc(stdin),fgetc(stdin) and flushing the stdin is not solving
the problem.
---
The Complete Source (with the bug) is here:
http://geocities.com/uthcode2/wumpus.c
''Hunt the Wumpus'' Game.
You can see the problem, when compiled and executed(on Linux,gcc)
Regards,
Senthil
我可以给出的最简单的例子来表达我面临的问题
面临的是:
#include< stdio.h>
int main(无效)
{
int int;
choice = getc(stdin);
while(choice =''Q'')
{
switch (选择)
{
case''n'':
putchar(''n'');
休息;
案例':
putchar('s'');
休息;
case''N'':
putchar(''N'');
休息;
案例''S'':
putchar(''S' ');
休息;
默认:
putchar(''%'');
break;
}
choice = getc(stdin);
}
putchar(''Q'');
返回0;
}
输出始终为%%;这意味着它无论如何进入默认的
部分。
The smallest example which I can give to express the the problem I am
facing is here:
#include<stdio.h>
int main(void)
{
int choice;
choice=getc(stdin);
while(choice=''Q'')
{
switch(choice)
{
case ''n'':
putchar(''n'');
break;
case ''s'':
putchar(''s'');
break;
case ''N'':
putchar(''N'');
break;
case ''S'':
putchar(''S'');
break;
default:
putchar(''%'');
break;
}
choice=getc(stdin);
}
putchar(''Q'');
return 0;
}
The Output is always %%; which means that it is entering the default
section no matter what.
在文章< 11 ***** *****************@g14g2000cwa.googlegroups .com> ;,
Phoe6< or ******* @ gmail.com>写道:
:我正在观察的问题是Switch命令的默认部分
:正在进入所有时间(第一次除外)。
:完整来源(有错误)在这里:
: http://geocities.com/uthcode2/wumpus.c
:''追捕Wumpus''游戏。
:你可以在编译和执行时看到问题(在Linux上,gcc)
当我使用SGI编译IRIX时,问题不会发生>
编译器或gcc。
我在代码中发现了一个错误:向南射击应该有+ b $ ba dy的+1代替 - 1。当然,将调试设置为1
会产生一个非常无趣的游戏。
-
当你的帖子全部单独/和一个用户在电话上/
有一个地方需要检查 - /上游!
当你匆忙/传播时一个担心/
有一个地方你可以发布 - /上游!
In article <11**********************@g14g2000cwa.googlegroups .com>,
Phoe6 <or*******@gmail.com> wrote:
:The Problem I am observing is the Default Section of the Switch command
:is being entering all the times (except for the first time).
:The Complete Source (with the bug) is here:
:http://geocities.com/uthcode2/wumpus.c
:''Hunt the Wumpus'' Game.
:You can see the problem, when compiled and executed(on Linux,gcc)
The problem does not occur when I compile on IRIX with SGI''s
compiler or with gcc.
I do find a bug in the code: shooting south should have
a dy of +1 instead of -1. And of course, having debug set to 1
makes for a pretty uninteresting game.
--
When your posts are all alone / and a user''s on the phone/
there''s one place to check -- / Upstream!
When you''re in a hurry / and propagation is a worry/
there''s a place you can post -- / Upstream!
在文章< 11 ****** ****************@g14g2000cwa.googlegroups .com> ;,
Phoe6< or ******* @ gmail.com>写道:
:我能给出的最简单的例子来表达我的问题
:饰面就在这里:
:while(choice =''Q'')
这是一个赋值语句,因此你可以选择
值''Q ''然后测试它是否为非零(当然是
)。 ''Q''不是列出的案例之一,所以它使用默认标签
。
您指向的代码(选择!=''Q'')测试
选择'的值而不是设置它。
-
什么是 ;终极模因?它会不会像Monty Python那样的b世界上最有趣的笑话,导致每个人遇到它们的死亡?想法*导致整个文化的破坏。
- 儿童的模因花园
In article <11**********************@g14g2000cwa.googlegroups .com>,
Phoe6 <or*******@gmail.com> wrote:
: The smallest example which I can give to express the the problem I am
:facing is here:
: while(choice=''Q'')
That''s an assignment statement, so you are clobbering choice with
the value ''Q'' and then testing whether that is non-zero (which
of course it is). ''Q'' is not one of the cases listed so it is going
to use the default tag.
The code you pointed to had while(choice!=''Q'') which tests
choice''s value instead of setting it.
--
What is "The Ultimate Meme"? Would it, like Monty Python''s
"The World''s Funniest Joke", lead to the deaths of everyone who
encountered it? Ideas *have* lead to the destruction of entire cultures.
-- A Child''s Garden Of Memes