案例标签不会减少为整数常量 [英] Case Label Does Not reduce to integer constant

查看:201
本文介绍了案例标签不会减少为整数常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么我的switch语句不起作用。
我把它砍掉了,因为它很长,但是用户应该输入一个字符到selection1&selection2然后我将它们作为参数传递给我的validSelection()函数。 validSelection函数应该测试有效条目的字符,并通过返回布尔值来拒绝所有其他字符。

I'm not understanding why my switch statement is not working. I've chopped it up since it was rather long, but the user is supposed to input a character to "selection1' & "selection2" and then I pass those as a parameter to my validSelection() function. The validSelection function is supposed to test the characters for the valid entries and deny all others by returning a boolean.

bool validSelection(char selection)
{
switch (selection)
{
case "R":
case "r":
case "P":
case "p":
case "S":
case "s":
    return true;
default:
    return false;
}
}

cin >> selection1;
cin >> selection2;
if (validSelection(selection1) && validSelection(selection2))

selection1和selection2都是char数据类型。

selection1 and selection2 both are char data types.

推荐答案

正如@CaptainObvlious已经指出的那样,双引号之间的任何内容都被视为字符串.Howe ver,你蚂蚁将整数值传递给开关。同样,你必须将字符放在单引号''

As @CaptainObvlious has already pointed out, anything between double-quotes "" is considered as string. However, you ant to pass the integer value to the switch. For the same, you have to put the characters inside single-quotes ''.

您可能需要更改我们的代码如下。

You may need to change our code as below.

bool validSelection(char selection)
{
    switch (selection)
    {
        case 'R':
        case 'r':
        case 'P':
        case 'p':
        case 'S':
        case 's':
            return true;
        default:
            return false;
    }
}

这篇关于案例标签不会减少为整数常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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