2

我正在开发一个游戏,我运行我的代码并得到错误“案例标签不会减少为整数常量”。我想我知道这意味着什么,但我该如何解决呢?这是我的代码:

#include<stdio.h>
#include<stdlib.h

int player_cash[3] = {50};
char job[][20] {
    'A',
    'B',
    'C',
    "Donate",
    "Go to work",
    "Exit"
};
int jobs;

int main()
{
    while(player_cash[0] > 0) {
        printf("Please type A, B, C, Donate, Go to work, or Exit\n");
        switch(jobs) {

            case 'A':
            player_cash[0]-=5;
            player_cash[1]+=5;
            printf("Cash=%i\n\n", player_cash[0]);
            continue;

            case 'B':
            player_cash[0]-=5;
            player_cash[2]+=5;
            printf("Cash=%i\n\n", player_cash[0]);
            continue;

            case 'C':
            player_cash[0]-=5;
            player_cash[3]+=5;
            printf("Cash=%i\n\n", player_cash[0]);
            continue;

            case "Donate":
            player_cash[0]-=15; //Error here
            player_cash[1]+=5;
            player_cash[2]+=5;
            player_cash[3]+=5;
            printf("Cash donated\n\n");
            printf("Cash=%i\n\n", player_cash[0]);
            continue;

            case "Go to work":
            player_cash[0]+=10; //Error here
            printf("Work done\n\n");
            printf("Cash=%i\n\n", player_cash[0]);
            continue;

            case "Exit":
            printf("Thanks for playing!\n\n"); //Error here
            break;

            default:
            printf("Does not compute");
            continue;
        }
    }
        getchar();
        return 0;
}

所以,我希望用户做的是输入其中一个选项,然后执行与之对应的操作。我该如何解决?

4

4 回答 4

7

您不能将字符串用作case表达式:

case "Donate":

只能使用整数表达式,所以 egcase 'A':是可以的。

从概念上讲,您有问题:jobsis an int,并且您正在测试字符串。如果您想允许用户输入字符串(超过一个字符),您需要保留一个字符串变量,并使用类似的东西fgets来获取完整的输入行。

于 2012-10-13T17:05:50.677 回答
1

您的一些案例标签是字符(类型char,用's 表示)。这些整数常量。

其他标签是字符串文字(用 表示"),其有效类型为const char *. 1这些不是整数常量,不能以这种方式使用。


1由于历史原因,它们通常可以像以前一样使用char *,但不要试图改变它们。要不然。

于 2012-10-13T17:07:49.117 回答
1

您不能将字符串与 c 进行比较。"hello" == "hello"不会按预期工作。switch 只对基本类型进行简单的 c 比较。

switch(jobs) {

        case 'A':
        player_cash[0]-=5;
        player_cash[1]+=5;
        printf("Cash=%i\n\n", player_cash[0]);
        continue;

        case 'B':
        player_cash[0]-=5;
        player_cash[2]+=5;
        printf("Cash=%i\n\n", player_cash[0]);
        continue;

        case 'C':
        player_cash[0]-=5;
        player_cash[3]+=5;
        printf("Cash=%i\n\n", player_cash[0]);
        continue;

        case 'D':
        player_cash[0]-=15; //Error here
        player_cash[1]+=5;
        player_cash[2]+=5;
        player_cash[3]+=5;
        printf("Cash donated\n\n");
        printf("Cash=%i\n\n", player_cash[0]);
        continue;

        case 'G':
        player_cash[0]+=10; //Error here
        printf("Work done\n\n");
        printf("Cash=%i\n\n", player_cash[0]);
        continue;

        case 'E':
        printf("Thanks for playing!\n\n"); //Error here
        break;

        default:
        printf("Does not compute");
        continue;
    }

由于您只读取 中的一个字符getch(),因此您可以比较该值。(但要求用户只输入一个字母,因为他输入了“捐赠”,getch() 将首先读取 D,返回,然后读取 o,等等。)

于 2012-10-13T17:08:42.740 回答
1
  1. 您的作业数组具有不一致的初始化程序(混合charconst char *

  2. 您不能将字符串文字用作大小写标签,因为 char 指针不是编译时常量。使用整数:

    enum jobType
    {
        jobA,
        jobB,
        jobC,
        jobDonate,
        jobGoToWork,
        jobExit,
        /* marker */
        jobInvalid
    };
    
    enum jobType typeOfJob(const char* const name)
    {
        int i;
        for (i=jobA; i!=jobInvalid; ++i)
            if (0 == strcmp(jobNames[i], name))
                return i;
        return i;
    }
    
  3. 此外,player_cash短 1 个元素(并且在索引 [3] 处写入超出范围)


代码示例还展示了如何避免一般gets错误,进行一些基本的行尾修剪和大小写不敏感比较(stricmp在 Windows 上,IIRC):http ://liveworkspace.org/code/227015a4e51126d55ca4eb1eea739b02

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int player_cash[4] = {50};

enum jobType
{
    jobA,
    jobB,
    jobC,
    jobDonate,
    jobGoToWork,
    jobExit,
    /* marker */
    jobInvalid
};

const char jobNames[][20] =
{
    "A",
    "B",
    "C",
    "Donate",
    "Go to work",
    "Exit"
};

enum jobType typeOfJob(const char* const name)
{
    int i;
    for (i=jobA; i!=jobInvalid; ++i)
#ifdef CASE_SENSITIVE
        if (0 == strcmp(jobNames[i], name))
#else
        if (0 == strcasecmp(jobNames[i], name))
#endif
            return i;
    return i;
}

const char* safer_gets()
{
    static char input[1024];
    char *p;
    const char* t;
    const char trimAt[] = "\r\n\t ";
    fgets(input, sizeof(input), stdin);

    for (t=trimAt; *t; ++t)
        while(p = strrchr(input, *t)) 
            *p = 0;

    return input;
}

int main()
{
    const char* input;
    while(player_cash[0] > 0)
    {
        printf("Please type A, B, C, Donate, Go to work, or Exit\n");
        input = safer_gets();

        switch(typeOfJob(input))
        {
        case jobA:
            player_cash[0]-=5;
            player_cash[1]+=5;
            printf("Cash=%i\n\n", player_cash[0]);
            continue;
        case jobB:
            player_cash[0]-=5;
            player_cash[2]+=5;
            printf("Cash=%i\n\n", player_cash[0]);
            continue;
        case jobC:
            player_cash[0]-=5;
            player_cash[3]+=5;
            printf("Cash=%i\n\n", player_cash[0]);
            continue;
        case jobDonate:
            player_cash[0]-=15; 
            player_cash[1]+=5;
            player_cash[2]+=5;
            player_cash[3]+=5;
            printf("Cash donated\n\n");
            printf("Cash=%i\n\n", player_cash[0]);
            continue;
        case jobGoToWork:
            player_cash[0]+=10;
            printf("Work done\n\n");
            printf("Cash=%i\n\n", player_cash[0]);
            continue;
        case jobExit:
            printf("Thanks for playing!\n\n");
            break;
        default:
            printf("Does not compute");
            continue;
        }
    }
    getchar();
    return 0;
}
于 2012-10-13T19:24:10.930 回答