使用int数组用于switch case

时间:2013-03-13 16:35:22

标签: c arrays switch-statement

我对C很新,我试图使用int数组作为这样的开关的选项号

void totalEntered(float * total, float * inputFigure)
{
    *total = *total + *inputFigure;
}

int main()
{
    float amountEntered = 0.00;
    float tot = 0.00;
    int option_numbers[3] = {1,2,3,4};
    float a_food = 0.00;
    float a_tab = 0.00;
    float a_trav = 0.00;
    float a_bill = 0.00;

    totalEntered(&tot, &amountEntered);



    printf("Please enter option number from below \n1.food\n2.tabacco\n3.travel\n4.bills\n");
    scanf("%i", option_numbers);
    switch(option_numbers)
    {
    case 1:
        printf("Please Enter an amount: ");
        scanf("%f", amountEntered);
        a_food = a_food + amountEntered;
        totalEntered(&tot, &amountEntered);
        printf("You have spent %f on food", a_food);
        break;
    case 2: /*ignore below haven't finish that*/
        a_tab = a_tab + amountEntered;
        printf("You have spent %.2f on tabacco", a_tab);
        break;
    case 3:
        a_trav = a_trav + amountEntered;
        printf("You have spent %.2f on travel", a_trav);
        break;
    case 4:
        a_bill = a_bill + amountEntered;
        printf("You have spent %.2f on travel", a_bill);
        break;
    default:
        printf("You have not input a category!");
        break;
    }
    return 0;


}

任何人都可以建议如何解决这个问题......我有点卡住了! :D我试图让用户选择一个==的数字,如果用户想要在食物下输入金额他/她必须选择选项1然后金额。希望这之前更有意义! 感谢

4 个答案:

答案 0 :(得分:5)

你无法将数组传递给switch(),你需要传递数组中的一个元素:

int item = 2; // or whatever

switch(option_numbers[item])
{

编辑:
根据您的评论,如果您想获取用户输入并让他们选择4个选项中的一个非常简单,那么该阵列是完全不需要的。

int selection = -1;
printf("Which option do you want? (1-4): ");
scanf("%d", &selection);

switch(selection)
{

旁注:

int option_numbers[3] = {1,2,3,4}; // that's not size 3, it's got 4 elements should 
                                   // have been:
                                   // int option_numbers[4] = {1,2,3,4}; 

答案 1 :(得分:2)

你的问题不清楚,所以我会回答我认为你想问的问题。首先,使用switch语句的更好方法是使用枚举。我很难解释,所以这里有一个代码示例:(这里是一个链接:How to define an enumerated type (enum) in C?

typedef enum { Food, Tobacco, Travel, Bills } Options;

大致(非常,非常粗略地)等同于:

int Food = 0;
int Tobacco = 1;
int Travel = 3;
int Bills = 4;

所以在switch语句中使用它的一个非常简单的方法是:

在你的main()之前的某个地方:

typedef enum { Food, Tobacco, Travel, Bills } Options;

然后在你的main()中:

Options myOption; // just as an example
printf("Pick your expense thingy (0, 1, 2, 3): ");
scanf("%d", &myOption);

switch (myOption) { // Now remember that Food = 0, Tobacco = 1 ...
    case Food: // food == 0
        // do food stuff
        break;

    case Tobacco:
        // do Tobacco stuff
        break;

    case Travel:
        // do Travel stuff
        break;

    case Bills:
        // do Bills stuff
        break;
}

---编辑---(2013年3月14日......哦,哇,这是pi天!)

好的,还有另外一种方法,当我今天早上打开电脑时,你可以做到这一点......你可以使用字符只是伪装的1字节数字这一事实;)...它更好,因为它不依赖于scanf(我真的不喜欢scanf lol):

char input;

printf("Pick your expense thingy (_f_ood, _t_obacco, t_r_avel, _b_ills): ");
input = getchar(); // this function just gets a char off of stdin and returns it

switch(input) {
    case 'f': case 'F': // note the single quotes, this tells the compiler that it is a single character and not a null terminated string
        // do food stuff
        break;

    case 't': case 'T': // we use both letters so that our comparisons are case-insensitive
        // do tobacco stuff
        break;

    case 'r': case 'R': // use r for travel because its the easiest way to differentiate the input
        // do travel stuff
        break;

    case 'b': case 'B': // so yeah...
        // do bills stuff
        break;

    default:
        printf("That was not a valid option :(");
}

答案 2 :(得分:0)

您正在尝试打开包含所有数字的数字数组。您应该对一个int变量进行切换,并指定一个特定的编号。

答案 3 :(得分:0)

int option_numbers[] = {1,2,3,4};
int i;

for (i = 0; i < sizeof(option_numbers)/sizeof(int); ++i)
{
    switch(option_numbers[i])
    {
        ....
    }
}

您可以根据需要改变上部索引边界。