-3

我对 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;


}

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

4

4 回答 4

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}; 
于 2013-03-13T16:37:00.943 回答
2

你的问题不清楚,所以我会回答我认为你想问的问题。首先,使用 switch 语句的更好方法是使用枚举。我很讨厌解释,所以这是一个代码示例:(这是一个链接:如何在 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 日......哦,哇,今天是圆周率日!)

好吧,当我今天早上打开电脑时,我突然想到了另一种方法可以做到这一点......你可以利用字符只是伪装的 1 字节数字这一事实;)......它更好,因为它没有t 依赖 scanf (我不太喜欢 scanf 哈哈):

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 :(");
}
于 2013-03-13T16:51:03.787 回答
0

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

于 2013-03-13T16:37:10.303 回答
0
int option_numbers[] = {1,2,3,4};
int i;

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

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

于 2013-03-13T16:44:40.047 回答