'if'语句控制开关案例 [英] 'if' statement to control switch case

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

问题描述

嗨伙计,


我正在尝试创建一个自定义CEdit控件,以便

我可以决定允许用户使用什么输入控件。我开始关闭
只允许浮点数然后添加支持

放入纬度/经度坐标。


我试过这个OnChar函数中的一小段代码,但

编译器抱怨在case _T(''W'')之后缺少'';'':"


开关(nChar)

{

if(m_bLATLON)//如果latlon没有negs和4个额外的字符。

{

case _T(''N''):

case _T(''E''):

case _T(''S' '):

case _T(''W''):

}

else

case _T( '' - ''); //如果不是latlon我们被允许negs。


case _T(''。'')://允许两个案例中的字符。

case _T (''0''):

case _T(''1''):

case _T('''''):

case _T(''3''):

case _T(''4''):

case _T('''''):

case _T(''''''):

case _T(''7''):

case _T('''''' ):

case _T(''9''):

case _T(''\ b''):

break;


默认:

返回;

}


现在我可以拥有''if''语句首先然后有两个

单独的switch语句,但我宁愿做上面的方法,因为它

''看起来''更优雅:o )

是否有一些神奇的方法可以做我想要的或者它只是打破

c ++的规则?


欢呼,


约翰。

Hi Folks,

I''m experimenting a little with creating a custom CEdit control so that
I can decide on what the user is allowed to type into the control. I
started off only allowing floating point numbers then added support for
putting in lat/lon coordinates.

I tried this little piece of code inside the OnChar function but
compiler complained about missing '';'' after "case _T(''W''):"

switch(nChar)
{
if(m_bLATLON) // if latlon then no negs and 4 extra chars.
{
case _T(''N''):
case _T(''E''):
case _T(''S''):
case _T(''W''):
}
else
case _T(''-''); // if not latlon we are allowed negs.

case _T(''.''): // allowed characters on both cases.
case _T(''0''):
case _T(''1''):
case _T(''2''):
case _T(''3''):
case _T(''4''):
case _T(''5''):
case _T(''6''):
case _T(''7''):
case _T(''8''):
case _T(''9''):
case _T(''\b''):
break;

default:
return;
}

now I could just have the ''if'' statement first and then have two
seperate switch statements but i would rather do the method above as it
''looks'' more elegante :o)
Is there some magic way to do what I am wanting or does it just break
the rules of c++ ?

Cheers,

John.

推荐答案

John写道:
John wrote:

嗨伙计,


我正在试验一下创建一个自定义CEdit控件,以便我可以决定允许用户输入

控件的内容。我开始只允许浮点数然后

增加了对lat / lon坐标的支持。


我在OnChar函数中尝试了这一小段代码但是

编译器抱怨在case _T(''W'')之后缺少'';'':"


switch(nChar)

{

if(m_bLATLON)//如果latlon没有negs和4个额外的字符。

{

case _T(''N''):

case _T(''E''):

case _T(''S''):

case _T(''W''):
Hi Folks,

I''m experimenting a little with creating a custom CEdit control so
that I can decide on what the user is allowed to type into the
control. I started off only allowing floating point numbers then
added support for putting in lat/lon coordinates.

I tried this little piece of code inside the OnChar function but
compiler complained about missing '';'' after "case _T(''W''):"

switch(nChar)
{
if(m_bLATLON) // if latlon then no negs and 4 extra chars.
{
case _T(''N''):
case _T(''E''):
case _T(''S''):
case _T(''W''):



你试过在这里插入分号吗?

Have you tried inserting the semicolon here?


}

其他

case _T('' - ''); //如果不是latlon我们被允许negs。
}
else
case _T(''-''); // if not latlon we are allowed negs.



分号在这里做什么?

What''s the semicolon doing here?


>

case _T(''。'')://允许两种情况下的字符。

case _T(''0''):

case _T(''' 1''):

case _T('''''):

case _T(''3''):

case _T(''4''):

case _T(''''''):

case _T(''''''):

case _T(''7''):

case _T(''''''):

case _T(''9''):

case _T(''\ b''):

休息;


默认:

返回;

}
>
case _T(''.''): // allowed characters on both cases.
case _T(''0''):
case _T(''1''):
case _T(''2''):
case _T(''3''):
case _T(''4''):
case _T(''5''):
case _T(''6''):
case _T(''7''):
case _T(''8''):
case _T(''9''):
case _T(''\b''):
break;

default:
return;
}



下次,请发布给您带来麻烦的真实代码。

Next time, do please post the real code that gives you trouble.


>

现在我可以首先使用''if''语句然后有两个

单独切换语句但是或者更倾向于采用上面的方法

它看起来更优雅:o)

是否有一些神奇的方法可以做我想要的或者只是打破它

c ++的规则?
>
now I could just have the ''if'' statement first and then have two
seperate switch statements but i would rather do the method above as
it ''looks'' more elegante :o)
Is there some magic way to do what I am wanting or does it just break
the rules of c++ ?



我认为它确实违反了规则,但你还没有把它完全带到

极端。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问

I think it does break the rules, but you haven''t taken it to the full
extreme yet.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


Victor Bazarov写道:
Victor Bazarov wrote:

John写道:
John wrote:

>> Hi Folks,

我正在尝试创建一个自定义CEdit控件,以便<我可以决定允许用户输入
控件的内容。我开始只允许浮点数然后
添加了对lat / lon坐标的支持。

我在OnChar函数中尝试了这一小段代码,但
编译器抱怨在'case _T(''W'')之后缺少'';'':'

开关(nChar)
{
如果(m_bLATLON)//如果是latlon然后没有negs和4个额外的字符。
{/> case _T(''N''):
case _T(''E''):
case _T('' S''):
case _T(''W''):
>>Hi Folks,

I''m experimenting a little with creating a custom CEdit control so
that I can decide on what the user is allowed to type into the
control. I started off only allowing floating point numbers then
added support for putting in lat/lon coordinates.

I tried this little piece of code inside the OnChar function but
compiler complained about missing '';'' after "case _T(''W''):"

switch(nChar)
{
if(m_bLATLON) // if latlon then no negs and 4 extra chars.
{
case _T(''N''):
case _T(''E''):
case _T(''S''):
case _T(''W''):




您是否尝试在此处插入分号?



Have you tried inserting the semicolon here?



是的,错误消失了,但代码的功能并不是
工作,即无论m_bLATLON是否为真/假它永远不会

承认任何情况。

Yes, and the error goes away but the functionality of the code doesn''t
work i.e. regardless of whether m_bLATLON is true/false it never
acknowledges any of the cases.


>
>

> }
其他
case _T('' - ''); //如果不是latlon我们被允许negs。
> }
else
case _T(''-''); // if not latlon we are allowed negs.




分号在这做什么?



What''s the semicolon doing here?



mmmhhh不确定 - 它不在我的代码中所以必须只是一个错字。

mmmhhh not sure - its not in my code so must just be a typo.


>
>

> case _T(''。'')://允许两种情况下的字符。
case _T(''0''):
case _T(''1''):
case _T('''''):
case _T(''3''):
case _T(''4''):
case _T('''''' ):
case _T('''''):
case _T(''7''):
case _T(''8''):
case _T (''9''):
case _T(''\ b''):
休息;

默认:
返回;
}
> case _T(''.''): // allowed characters on both cases.
case _T(''0''):
case _T(''1''):
case _T(''2''):
case _T(''3''):
case _T(''4''):
case _T(''5''):
case _T(''6''):
case _T(''7''):
case _T(''8''):
case _T(''9''):
case _T(''\b''):
break;

default:
return;
}




下次,请发布给您带来麻烦的实际代码。



Next time, do please post the real code that gives you trouble.



这是真正的代码 - 它只有一个错字。我不想发布

完整的OnChar功能,因为我认为有人会认为这是一个CEdit功能并且让我在发布错误

组!

This is the real code - it just had that one typo. I didn''t want to post
the entire OnChar function that I overode as I thought someone would
see that it was a CEdit function and flame me for posting in the wrong
group!


>
>

> ;>现在我可以首先使用''if''语句,然后有两个
单独的switch语句,但我宁愿做上面的方法,因为
它看起来更'优雅:o )
是否有一些神奇的方法可以做我想要的事情,还是只是打破了c ++的规则?
>>now I could just have the ''if'' statement first and then have two
seperate switch statements but i would rather do the method above as
it ''looks'' more elegante :o)
Is there some magic way to do what I am wanting or does it just break
the rules of c++ ?




我认为它确实违反了规则,但你还没有把它带到完整的

极端。


V



I think it does break the rules, but you haven''t taken it to the full
extreme yet.

V


John写道:
John wrote:

Victor Bazarov写道:
Victor Bazarov wrote:

> John写道:
>John wrote:

>> Hi Folks,

我正在尝试创建一个自定义CEdit控件,以便我可以决定允许用户输入
控件的内容。我开始只允许浮点数然后
添加了对lat / lon坐标的支持。

我在OnChar函数中尝试了这一小段代码,但
编译器抱怨在'case _T(''W'')之后缺少'';'':'

开关(nChar)
{
如果(m_bLATLON)//如果是latlon然后没有negs和4个额外的字符。
{/> case _T(''N''):
case _T(''E''):
>>Hi Folks,

I''m experimenting a little with creating a custom CEdit control so
that I can decide on what the user is allowed to type into the
control. I started off only allowing floating point numbers then
added support for putting in lat/lon coordinates.

I tried this little piece of code inside the OnChar function but
compiler complained about missing '';'' after "case _T(''W''):"

switch(nChar)
{
if(m_bLATLON) // if latlon then no negs and 4 extra chars.
{
case _T(''N''):
case _T(''E''):



[..]


好​​的,这就是我试过的。它编译(在VC ++和Comeau上),

和效果(在VC ++上)是'if'基本上被忽略了:


#包含< iostream>

使用命名空间std;


int main(int argc,char * argv [])

{

开关(argc)

{

if(argv [0] [0] ==''C''){

案例0:

案例1:

cout<< 0或1 \ n;

休息;

}

else {

案例2:

cout<< 2 \ n;

休息;

默认值:

cout<< 3或更多\ nn;

}

}

}


试试它。


V

-

请在通过电子邮件回复时删除资金''A' />
我没有回复最热门的回复,请不要问

[..]

OK, this is what I tried. It compiles (on both VC++ and Comeau),
and the effect (on VC++) is that the ''if'' is essentially ignored:

#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
switch (argc)
{
if (argv[0][0] == ''C'') {
case 0:
case 1:
cout << "0 or 1\n";
break;
}
else {
case 2:
cout << "2\n";
break;
default:
cout << "3 or more\n";
}
}
}

Try it.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


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

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