Wearin
Wearin

Reputation: 27

Switch statement keeps using default case no matter what - C beginner

//This code is supposed to add or subtract and keep track of four different beer brands with individual ID's. Using -1 does exit, but using any of the ID numbers 1-4 causes the program to execute the defualt case.

#include <stdio.h>

int main(){
int inv1;
int inv2;
int inv3;
int inv4;

printf("Pleas enter the beginning inventory of Piels. \n");
scanf("%d", &inv1);
printf("Pleas enter the beginning inventory of Coors. \n");
scanf("%d", &inv2);
printf("Pleas enter the beginning inventory of Bud. \n");
scanf("%d", &inv3);
printf("Pleas enter the beginning inventory of Iron City. \n");
scanf("%d", &inv4);

printf("Please enter the ID number. \n Piels - 1 \n Coors - 2 \n Bud - 3 \n Iron City - 4 \n Enter -1 to exit \n"); 

int id;

scanf("%d", &id);
while( id != -1){

int amount;

    switch (id) {
    case '1': 
         printf("Enter the change in amount. It will be a negative if the amount is sold, or positive if the amount is purchased \n");
         scanf("%d", &amount);
         inv1 += amount;
         break;
    case '2': 
         printf("Enter the change in amount. It will be a negative if the amount is sold, or positive if the amount is purchased \n");
         scanf("%d", &amount);
         inv2 += amount;
         break;
    case '3': 
         printf("Enter the change in amount. It will be a negative if the amount is sold, or positive if the amount is purchased \n");
         scanf("%d", &amount);
         inv3 += amount;
         break;
    case '4': 
         printf("Enter the change in amount. It will be a negative if the amount is sold, or positive if the amount is purchased \n");
         scanf("%d", &amount);
         inv4 += amount;
         break;
    default:
        printf("Error: That ID Number is not an option. \n");
        break;
                  }


printf("Please enter the ID number. \n Piels - 1 \n Coors - 2 \n Bud - 3 \n Iron City - 4 \n Enter -1 to exit \n");
    scanf("%d", &id);
}
    return 0;

}

Upvotes: 0

Views: 1264

Answers (3)

user3629249
user3629249

Reputation: 16540

There seems to be a mis-understanding, as shown in the code, between a character value and a integer value.

a character, like '1' has the value 0x31.

while a integer value like 1 has the value 0x00000001.

For the switch/cases, the case values need to be integers, so do not wrap those values in single quotes as those single quotes indicate character literals rather than integer literals.

Upvotes: 1

Ian
Ian

Reputation: 30813

Since you read your input as integer:

scanf("%d", &id); //note the %d

Then the id will have a value of integer, converted from the string which you input in your Console.

Example: 
Input: 41 //this is char '4' and '1', having value of 0x34 (52) and 0x31 (49) respectively
Result: id = 41 //(not resulting in char* but value of 41)

If you want to use character, then you should do this instead:

char id;
scanf(" %c", &id); //note the %c, might also be better if used with early space to avoid \n from the input buffer

Such that it will read your input as character

Example:
Input: 4 //this is char '4' (having value of 0x34 (52))
Result: id = '4' or id = 52 //not resulting in actual value of 4, but char '4' or value of 52 (0x34)

Then your switch:

switch(id){
    case '4': //this has actual value of 52 (or 0x34), check ASCII table
        break;
    // and so on, 
}

will be fine.

Note that this will limit your option from '0'-'9' though, since they are the only available ASCII chars.

So, it is still best to use scanf("%d", &id);

Simply remove the ' in your switch:

int id;
scanf("%d, &id);
switch(id){
    case 1: //this has actual integer value of 1, not ASCII '1' = 0x31 (or 49)
        break;
    // and so on, will be fine
}

Then it will also work.

Upvotes: 1

zakaiter
zakaiter

Reputation: 439

Remove quotes and try from switch condition

 switch (id) {
        case 1: 
             printf("Enter the change in amount. It will be a negative if the amount is sold, or positive if the amount is purchased \n");
             scanf("%d", &amount);
             inv1 += amount;
             break;
        case 2: 
             printf("Enter the change in amount. It will be a negative if the amount is sold, or positive if the amount is purchased \n");
             scanf("%d", &amount);
             inv2 += amount;
             break;
        case 3: 
             printf("Enter the change in amount. It will be a negative if the amount is sold, or positive if the amount is purchased \n");
             scanf("%d", &amount);
             inv3 += amount;
             break;
        case 4: 
             printf("Enter the change in amount. It will be a negative if the amount is sold, or positive if the amount is purchased \n");
             scanf("%d", &amount);
             inv4 += amount;
             break;
        default:
            printf("Error: That ID Number is not an option. \n");
            break;
                      }

Upvotes: 4

Related Questions