开关 [英] switches

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

问题描述

是否可以在交换机内调用某个功能?我的函数调用是

以某种方式绕过。

int startmenu()

{

exitgame = 0;


int line_num;

for(line_num = 0; line_num< 12; line_num ++)

{

puts(menu [line_num]);

}


printf(" Command:");


int c = tolower(getchar());

开关(c)

{

case('s' ''):

/ *开始游戏* /

puts(开始游戏......);

printf(" ;输入你的名字:");

scanf("%s",cname);

puts("(F)ighter');

puts((S)orceress);

puts("(T)hief");

printf("输入你的班级:");

class(); / *<< - 此函数调用* /

休息;

case(''l''):

/ *加载游戏* /

puts(加载尚未确定。);

break;

case(''q ''):

/ *退出游戏* /

休息;

案例EOF:

/ *哦不!问题! * /

休息;

//默认:

// / *错误的选择* /

// puts(选择错误,再试一次。;

// break;

}

}

Is it possible to call a function within a switch? My function call is
bypassed somehow.

int startmenu()
{
exitgame = 0;

int line_num;
for (line_num = 0; line_num < 12; line_num++)
{
puts (menu[line_num]);
}

printf ("Command: ");

int c = tolower (getchar());
switch (c)
{
case (''s''):
/* Start Game */
puts ("Starting game...");
printf ("Enter your name: ");
scanf ("%s", cname);
puts ("(F)ighter");
puts ("(S)orceress");
puts ("(T)hief");
printf ("Enter your class: ");
class(); /* <<-- This function call */
break;
case (''l''):
/* Load Game */
puts ("Loading has not been established yet.");
break;
case (''q''):
/* Quit Game */
break;
case EOF:
/* Oh, no! Problems! */
break;
// default:
// /* Incase of Wrong Choice */
// puts ("Wrong choice, try again.");
// break;
}
}

推荐答案

Jonathan H. Justvig写道:
Jonathan H. Justvig wrote:

是否可以在交换机内调用函数?我的函数调用是

以某种方式绕过。
Is it possible to call a function within a switch? My function call is
bypassed somehow.



是的,你已经在代码中生成了几个(puts,printf,scanf)。


除非你发布一个自包含且可编辑的例子来显示你的

问题,否则我们将无法提供帮助。

Yes and you''ve made several in your code (puts,printf,scanf).

Unless you post a self contained and compilable example that shows your
problem, we won''t be able to help.


int startmenu()

{

exitgame = 0;


int line_num;

for(line_num = 0; line_num< 12; line_num ++)

{

puts(menu [line_num]);

}


printf(" Command:");


int c = tolower(getchar());

开关(c)

{

case('s''):

/ *开始游戏* /

puts(开始游戏......);

printf("输入你的名字:");

scanf("%s",cname);

put("(F)ighter');

put("(S)orceress");

puts("(T)hief");

printf(" Enter your class:" );

class(); / *<< - 此函数调用* /

休息;

case(''l''):

/ *加载游戏* /

puts(加载尚未确定。);

break;

case(''q ''):

/ *退出游戏* /

休息;

案例EOF:

/ *哦不!问题! * /

休息;

//默认:

// / *错误的选择* /

// puts(选择错误,再试一次。;

// break;

}

}
int startmenu()
{
exitgame = 0;

int line_num;
for (line_num = 0; line_num < 12; line_num++)
{
puts (menu[line_num]);
}

printf ("Command: ");

int c = tolower (getchar());
switch (c)
{
case (''s''):
/* Start Game */
puts ("Starting game...");
printf ("Enter your name: ");
scanf ("%s", cname);
puts ("(F)ighter");
puts ("(S)orceress");
puts ("(T)hief");
printf ("Enter your class: ");
class(); /* <<-- This function call */
break;
case (''l''):
/* Load Game */
puts ("Loading has not been established yet.");
break;
case (''q''):
/* Quit Game */
break;
case EOF:
/* Oh, no! Problems! */
break;
// default:
// /* Incase of Wrong Choice */
// puts ("Wrong choice, try again.");
// break;
}
}



-

Ian Collins。


--
Ian Collins.




Jonathan H Justvig写道:

Jonathan H. Justvig wrote:

是否可以在交换机内调用函数?我的函数调用是

以某种方式绕过。

int startmenu()

{

exitgame = 0;


int line_num;

for(line_num = 0; line_num< 12; line_num ++)

{

puts(menu [line_num]);

}


printf(" Command:");


int c = tolower(getchar());

开关(c)

{

case('s' ''):

/ *开始游戏* /

puts(开始游戏......);

printf(" ;输入你的名字:");

scanf("%s",cname);

puts("(F)ighter');

puts((S)orceress);

puts("(T)hief");

printf("输入你的班级:");

class(); / *<< - 此函数调用* /

休息;

case(''l''):

/ *加载游戏* /

puts(加载尚未确定。);

break;

case(''q ''):

/ *退出游戏* /

休息;

案例EOF:

/ *哦不!问题! * /

休息;

//默认:

// / *错误的选择* /

// puts(选择错误,再试一次。;

// break;

}

}
Is it possible to call a function within a switch? My function call is
bypassed somehow.

int startmenu()
{
exitgame = 0;

int line_num;
for (line_num = 0; line_num < 12; line_num++)
{
puts (menu[line_num]);
}

printf ("Command: ");

int c = tolower (getchar());
switch (c)
{
case (''s''):
/* Start Game */
puts ("Starting game...");
printf ("Enter your name: ");
scanf ("%s", cname);
puts ("(F)ighter");
puts ("(S)orceress");
puts ("(T)hief");
printf ("Enter your class: ");
class(); /* <<-- This function call */
break;
case (''l''):
/* Load Game */
puts ("Loading has not been established yet.");
break;
case (''q''):
/* Quit Game */
break;
case EOF:
/* Oh, no! Problems! */
break;
// default:
// /* Incase of Wrong Choice */
// puts ("Wrong choice, try again.");
// break;
}
}



当我们不知道class()

函数是什么时,我们无法提供帮助。 />
我建议的是,在class()函数的顶部,输入一个

行,比如


printf(" ; Debug 1 \ n");


然后运行程序,你可以检查它是否打印Debug 1。在哪里你

应该调用class()。如果没有,那么确实class()不会被调用(给出上面的代码会很奇怪)。

如果是这样的话,移动printf进一步向下,使用它来感觉到

问题出在哪里。这样做几次,在你知道它之前,你会发现你自己盯着一个for循环,紧接着是一个错误的

分号,或其他类似的东西。

It''s impossible for us to help when we don''t know what the "class()"
function does.
What I would recommend is, at the top of the class() function, put in a
line like

printf( "Debug 1\n" );

Then run the program and you can check if it prints "Debug 1" where you
think class() should be called. If not, then indeed class() isn''t
getting called (which, given the above code, would be pretty weird).
If so, move the printf line further down, using it to "feel out" where
the problem is. Do this a few times and before you know it you''ll find
yourself staring at a for loop followed immediately by a misplaced
semicolon, or something else like that.


" Jonathan H. Justvig" < cr ****** @ cox.netwrites:
"Jonathan H. Justvig" <cr******@cox.netwrites:

是否可以在交换机内调用函数?
Is it possible to call a function within a switch?



当然。

Of course.


我的函数调用以某种方式被绕过。


int startmenu()

{

exitgame = 0;


int line_num;

for(line_num = 0; line_num< 12; line_num ++)

{

puts(menu [line_num]);

}


printf(" Command:");


int c = tolower(getchar());
My function call is bypassed somehow.

int startmenu()
{
exitgame = 0;

int line_num;
for (line_num = 0; line_num < 12; line_num++)
{
puts (menu[line_num]);
}

printf ("Command: ");

int c = tolower (getchar());



getchar()返回stdin的下一个字符(如果没有更多输入可用,则返回EOF
)。如果你键入s,系统就不会处理你的

输入,直到你点击返回键,这将导致getchar()返回

返回''\ n''。


我不知道这是不是你的问题,但可能是与此相关的



为了帮助追踪它,添加一个默认情况,显示

字符的值(使用'''%c''"如果它是可打印的,%d,如果它不是)。

getchar() returns the next character from stdin (or EOF if there''s no
more input available). If you type ''s'', the system won''t process your
input until you hit the return key, which will then cause getchar() to
return ''\n''.

I don''t know whether that''s your problem, but it might be something
related to that.

To help track it down, add a default case that shows the value of the
character (use "''%c''" if it''s printable, "%d" if it isn''t).


switch(c)

{

case('s''):
switch (c)
{
case (''s''):



[code snipped]

[code snipped]


}
}



-

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

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

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

--
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.


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

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