无论什么原因,Switch语句都保持使用默认情况--C初学者

时间:2016-02-17 04:25:17

标签: c while-loop switch-statement

//此代码应添加或减少并跟踪具有个人ID的四种不同啤酒品牌。使用-1确实退出,但使用任何ID号1-4会导致程序执行defualt情况。

#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;

}

3 个答案:

答案 0 :(得分:4)

  

删除引号并尝试切换条件

 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;
                      }

答案 1 :(得分:1)

因为您将输入读作整数:

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

然后id将具有整数值,从您在控制台中输入的string转换而来。

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)

如果你想使用角色,那么你应该这样做:

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

这样它会将您的输入读作字符

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)

然后你的开关:

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

会好的。

请注意,这会限制您选择'0'-'9',因为它们是唯一可用的ASCII字符。

因此,最好使用scanf("%d", &id);

只需移除'中的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
}

然后它也会起作用。

答案 2 :(得分:1)

如代码所示,在字符值和整数值之间似乎存在误解。

一个角色,比如&#39; 1&#39;值为0x31。

而像1这样的整数值的值为0x00000001。

对于switch / cases,case值必须是整数,所以不要将这些值包装在单引号中,因为这些单引号表示字符文字而不是整数文字。