带有char的Switch语句在C中总是返回0

带有char的Switch语句在C中总是返回0,c,char,switch-statement,C,Char,Switch Statement,由于某些原因,switch语句总是返回一个零的值,输入值在开关中正确匹配,但由于某些原因,返回值默认为零,使colorx=0。感谢您在高级课程中的帮助 #include <stdio.h> #include <stdlib.h> #include <math.h> double Band(char code); int main() { char code1, code2, code3, code4, code5; double r

由于某些原因,switch语句总是返回一个零的值,输入值在开关中正确匹配,但由于某些原因,返回值默认为零,使colorx=0。感谢您在高级课程中的帮助

#include <stdio.h>
#include <stdlib.h>
#include <math.h>


double  Band(char code);

int main()
{
char     code1, code2, code3, code4, code5;     
double  resistance;
double  color1 = 0, color2 = 0, color3 = 0, color4 = 0, color5 = 0;  
int     flag, Lines, i;
FILE *ifp, *ofp;
//char outputFilename[] = "C:\\Users\Kevin\\Desktop\\resistorOutput.txt";
ifp = fopen("C:\\Users\\Kevin\\Desktop\\resistorInput.txt", "r");
//ofp = fopen("C:\\Users\\Kevin\\Desktop\\resistorOutput.txt", "w");

rewind(ifp);
fscanf(ifp, "%d\n", &Lines);
for(i=1; i<=Lines; i++)
{
printf("Lines = %d\n", Lines);
fscanf(ifp, "%c%c%c%c%c\n", &code1, &code2, &code3, &code4, &code5);
printf("code1 = %c\n", code1);
printf("code2 = %c\n", code2);
printf("code3 = %c\n", code3);
printf("code4 = %c\n", code4);
printf("code5 = %c\n", code5);
color1 = Band( code1);
color2 = Band( code2);
color3 = Band( code3);
color4 = Band( code3);
color5 = Band( code3);
printf("color1 = %d\n", color1);
printf("color2 = %d\n", color2);
printf("color3 = %d\n", color3);
printf("color4 = %d\n", color4);
printf("color5 = %d\n", color5);
}
}


double Band(char code)
{
printf("Switch Code = %c\n", code );
switch ( code )
      {
case 'A':{
      printf("case a\n");
      return 0.0;
     }
case 'B':{
     printf("case b\n");
     return 1.0;
     }
case 'C':{
      printf("case c\n");
      return 2.0;
     }
case 'D':{
      printf("case d\n");
      return 3.0;
     }
case 'E':{
      printf("case e\n");
      return 4.0;
     }
case 'F':{
      printf("case f\n");
      return 5.0;
    }
case 'G':{
      printf("case g\n");
      return 6.0;
    }
case 'H':{
      printf("case h\n");
      return 7.0;
    }
case 'I':{
     printf("case i\n");
     return 8.0;
   }
case 'J':{
      printf("case j\n");
      return 9.0;
    }
case 'K':{
     printf("case k\n");
     return 10.0;
    }
case 'L':{
      printf("case l\n");
      return 11.0;
    }
default:{
      printf("case default\n");
      return 11.0;
        }

}
}
#包括
#包括
#包括
双波段(字符码);
int main()
{
字符代码1、代码2、代码3、代码4、代码5;
双电阻;
双色1=0,色2=0,色3=0,色4=0,色5=0;
int标志,行,i;
文件*ifp,*ofp;
//char outputFilename[]=“C:\\Users\Kevin\\Desktop\\resistorOutput.txt”;
ifp=fopen(“C:\\Users\\Kevin\\Desktop\\resistorInput.txt”、“r”);
//ofp=fopen(“C:\\Users\\Kevin\\Desktop\\resistorOutput.txt”、“w”);
倒带(ifp);
fscanf(ifp、%d\n、&line);

对于(i=1;i,编译器说得非常清楚:

1.c:40:25: warning: format specifies type 'int' but the argument has type 'double' [-Wformat]
printf("color1 = %d\n", color1);
开关工作正常,函数
Band()
可能返回正确的值,但它们是
double
,您尝试将它们打印为整数

printf()
格式从
%d
更改为
%f
,它将正常工作


还请注意,
color4
color5
是使用
code3
计算的,这可能不是故意的,而是复制粘贴错误。

我看到您的函数返回双精度类型,但在打印时使用“%d”进行整数打印。使用%f或%lf(ISO C不支持)用于打印双精度类型。对我来说,它工作正常。

它没有返回0,您正在将双精度打印为整数。请将printf(“color1=%d\n”,color1)更改为printf(“color1=%lf\n”,color1)@Sayutee,
%f
比那里的
%lf
好。谢谢你!!!!!!!!!!!!!!!!!!!!!Sayutee,你为什么不把它作为一个答案发布呢?我本想把你的作为一个答案@Sayutee,但你没有把它作为一个答案发布。
%lf
自ISO C99以来是标准化的。我“吃了”90之后的C字母。谢谢!我提到了C90,除非你指定编译器考虑C99或C11。