-3

I am fairly new to C and I am trying to use an int array as option numbers for a switch like this

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;


}

can anyone advise how to go about this... I am a bit stuck! :D I am trying at get the user to choose a number which is == to a case if the user wants to enter an amount under food he/she has to pick option 1 and then an amount. Hope this makes more sense that before! Thanks

Jalcock501
  • 389
  • 2
  • 6
  • 18

4 Answers4

5

You can't pass an array into a switch() you need to pass one of the elements in the array:

int item = 2; // or whatever

switch(option_numbers[item])
{

EDIT:
Based on your comment, if you want to take user input and have them pick one of the 4 options it's pretty simple, the array is totally unneeded.

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

switch(selection)
{

side note:

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}; 
Mike
  • 47,263
  • 29
  • 113
  • 177
  • @Jalcock501 - Check the edit to my answer, if I understand correctly what you're talking about, the array is totally pointless here, you just need to get the user's input in a single variable, then you can use the `switch()` as you were doing. note: I skipped the error checking, but you shouldn't ;) – Mike Mar 13 '13 at 16:52
2

You're question is unclear, so I'll answer what I think you are trying to ask. First off, a better way to use switch statements is with an enum. I suck at explaining, so here is a code example: (here's a link: How to define an enumerated type (enum) in C?)

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

is roughly (very, very roughly) equivalent to:

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

so a really easy way to use that in a switch statement is:

somewhere before your main():

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

and then in your 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;
}

--- Edit --- (3/14/2013 ... oh wow, it's pi day!)

Ok, there is another way you could do it that dawned on me as I turned my computer on this morning... you can use the fact that characters are just 1 byte numbers in disguise ;) ... its better because it doesn't rely on scanf (I don't really like 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 :(");
}
Community
  • 1
  • 1
George Mitchell
  • 1,158
  • 2
  • 11
  • 24
0

You're trying to switch on an array of numbers that has all numbers in it. You should do the switch on an int variable, with a specific number assigned.

mjuarez
  • 16,372
  • 11
  • 56
  • 73
0
int option_numbers[] = {1,2,3,4};
int i;

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

You can vary the upper index boundary as you need.

Valeri Atamaniouk
  • 5,125
  • 2
  • 16
  • 18