我的 switch 语句总是返回第一种情况?
My switch statement is always returning the first case?
我在使用这段代码时遇到了一些问题。我对 switch 语句和枚举类型还很陌生,所以可能有点过头了。我设法通过它来输入 switch 语句,但它一直返回第一种情况。有什么想法吗?
#include <stdio.h>
#include <string.h>
enum express {ADD, SUB, AND, OR, XOR, SHL, SHR};
express m_express;
express switchint(char *str);
int main(){
unsigned int n1=0x00;
unsigned int n2=0x00;
char action[5];
printf("Enter an expression: ");
scanf("%x, %s, %x", &n1, action, &n2);
m_express=switchint(action);
unsigned int result;
switch(m_express){
case ADD:
printf("add works");
break;
case SUB:
printf("SUB works");
break;
default:
printf("Default");
break;
}
}
express switchint(char *str){
if( strcmp(str, "add")){
return ADD;
}
else if ( strcmp(str, "sub")){
return SUB;
}
else if ( strcmp(str, "and")){
return AND;
}
else if ( strcmp(str, "or")){
return OR;
}
else if ( strcmp(str, "xor")){
return XOR;
}
else if ( strcmp(str, "shl")){
return SHL;
}
else {
return SHR;
}
}
我还没有编写我需要的其余开关案例。非常感谢任何解决此问题的帮助!
strcmp
returns 如果两个字符串相等则为 0。你应该重写你的支票:
if( !strcmp(str, "add"))
{
}
else if ( !strcmp(str, "sub")){
return SUB;
}
strcmp returns 0
当两个字符串相等时。所以你在 switchint
函数中的比较应该是:
if(!strcmp(str, "add")) { ... }
其他比较也是如此。
根据C标准中函数的描述(7.23.4.2中的strcmp函数)
3 The strcmp function returns an integer greater than, equal
to, or less than zero, accordingly as the string pointed to by
s1 is greater than, equal to, or less than the string pointed
to by s2.
所以你至少应该这样写
express switchint( const char *str){
^^^^^
if( strcmp(str, "add") == 0 ){
return ADD;
}
else if ( strcmp(str, "sub") == 0 ){
return SUB;
}
// and so on
我在使用这段代码时遇到了一些问题。我对 switch 语句和枚举类型还很陌生,所以可能有点过头了。我设法通过它来输入 switch 语句,但它一直返回第一种情况。有什么想法吗?
#include <stdio.h>
#include <string.h>
enum express {ADD, SUB, AND, OR, XOR, SHL, SHR};
express m_express;
express switchint(char *str);
int main(){
unsigned int n1=0x00;
unsigned int n2=0x00;
char action[5];
printf("Enter an expression: ");
scanf("%x, %s, %x", &n1, action, &n2);
m_express=switchint(action);
unsigned int result;
switch(m_express){
case ADD:
printf("add works");
break;
case SUB:
printf("SUB works");
break;
default:
printf("Default");
break;
}
}
express switchint(char *str){
if( strcmp(str, "add")){
return ADD;
}
else if ( strcmp(str, "sub")){
return SUB;
}
else if ( strcmp(str, "and")){
return AND;
}
else if ( strcmp(str, "or")){
return OR;
}
else if ( strcmp(str, "xor")){
return XOR;
}
else if ( strcmp(str, "shl")){
return SHL;
}
else {
return SHR;
}
}
我还没有编写我需要的其余开关案例。非常感谢任何解决此问题的帮助!
strcmp
returns 如果两个字符串相等则为 0。你应该重写你的支票:
if( !strcmp(str, "add"))
{
}
else if ( !strcmp(str, "sub")){
return SUB;
}
strcmp returns 0
当两个字符串相等时。所以你在 switchint
函数中的比较应该是:
if(!strcmp(str, "add")) { ... }
其他比较也是如此。
根据C标准中函数的描述(7.23.4.2中的strcmp函数)
3 The strcmp function returns an integer greater than, equal to, or less than zero, accordingly as the string pointed to by s1 is greater than, equal to, or less than the string pointed to by s2.
所以你至少应该这样写
express switchint( const char *str){
^^^^^
if( strcmp(str, "add") == 0 ){
return ADD;
}
else if ( strcmp(str, "sub") == 0 ){
return SUB;
}
// and so on