无论如何,Switch 语句都会使用默认大小写 - C 初学者
Switch statement keeps using default case no matter what - C beginner
//此代码应该添加或减去并跟踪四个具有个人 ID 的不同啤酒品牌。使用 -1 会退出,但使用 ID 号 1-4 中的任何一个都会导致程序执行默认情况。
#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;
}
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;
}
由于您将输入读取为整数:
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
}
那也行。
如代码所示,字符值和整数值之间似乎存在误解。
一个字符,如“1”,其值为 0x31。
而像 1 这样的整数值的值为 0x00000001。
对于 switch/cases,大小写值必须是整数,因此不要将这些值用单引号引起来,因为这些单引号表示字符文字而不是整数文字。
//此代码应该添加或减去并跟踪四个具有个人 ID 的不同啤酒品牌。使用 -1 会退出,但使用 ID 号 1-4 中的任何一个都会导致程序执行默认情况。
#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;
}
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;
}
由于您将输入读取为整数:
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
}
那也行。
如代码所示,字符值和整数值之间似乎存在误解。
一个字符,如“1”,其值为 0x31。
而像 1 这样的整数值的值为 0x00000001。
对于 switch/cases,大小写值必须是整数,因此不要将这些值用单引号引起来,因为这些单引号表示字符文字而不是整数文字。