Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 程序跳过开关盒中的所有盒,并转到默认值_C_Switch Statement - Fatal编程技术网

C 程序跳过开关盒中的所有盒,并转到默认值

C 程序跳过开关盒中的所有盒,并转到默认值,c,switch-statement,C,Switch Statement,我正在尝试构建一个使用用户输入的小程序,然后使用开关盒来执行适合用户输入的操作。 我一直在努力寻找我的程序中的错误,但运气不佳。很明显,我遗漏了一些东西 这是代码,你能帮我找出它有什么问题吗 #include <stdio.h> #define A 8.5 #define B 7.6 #define C 7.7 #define D 7.7 int main () { char fuel; float amount,total; printf("plea

我正在尝试构建一个使用用户输入的小程序,然后使用开关盒来执行适合用户输入的操作。 我一直在努力寻找我的程序中的错误,但运气不佳。很明显,我遗漏了一些东西

这是代码,你能帮我找出它有什么问题吗

#include <stdio.h>
#define A 8.5
#define B 7.6
#define C 7.7
#define D 7.7

int main ()
{


    char fuel;
    float amount,total;

    printf("please enter the desired fuel\n");
    scanf_s("%c",&fuel);

   switch(fuel)
   {
   case 'A' :
         printf("how many liters of fuel do you want?\n");
         scanf_s("%f",&amount);
         total=amount*A;
         if(total>150)
         {
             printf("the total price to pay is %.3f: \nYou have won a newspaper", total);
         }
         else
         printf("the total price to pay is %.3f", total);
         break;

      case 'B' :
          printf("how many liters of fuel do you want?\n");
         scanf_s("%f",&amount);
         total=amount*B;
         if(total>150)
             printf("the total price to pay is %f: \nYou have won a newspaper", total);
         else
         printf("the total price to pay is %.3f", total);

          break;
      case 'C' :
         printf("how many liters of fuel do you want?\n");
         scanf_s("%f",&amount);
         total=amount*C;
         if(total>150)
             printf("the total price to pay is %f: \nYou have won a newspaper", total);
         else
         printf("the total price to pay is %.3f", total);
         break;

      case 'D' :
         printf("how many liters of fuel do you want?\n");
         scanf_s("%f",&amount);
         total=amount*D;
         if(total>150)
             printf("the total price to pay is %f: \nYou have won a newspaper", total);
         else
         printf("the total price to pay is %f", total);

      break;

      default: 
      printf("no\n");
      break;
   }



}
即使我输入“A”、“B”、“C”或“D”,它也会进入默认值,而不是适当的情况。 谢谢您的帮助。

您没有正确使用扫描功能。根据其报告:

与scanf和wscanf不同,scanf_和wscanf_需要缓冲区大小 为c、c、s、s或字符串类型的所有输入参数指定 包含在[]中的控件集。缓冲区大小(以字符为单位)为 作为指向的指针后面的附加参数传递 缓冲区或变量

还应检查错误,因此您应执行以下操作:

char fuel;
if (scanf_s("%c", &fuel, 1) != 1)  {
    puts("Error from scanf_s");
    return 1;
}
您没有正确使用scanf_函数。根据其报告:

与scanf和wscanf不同,scanf_和wscanf_需要缓冲区大小 为c、c、s、s或字符串类型的所有输入参数指定 包含在[]中的控件集。缓冲区大小(以字符为单位)为 作为指向的指针后面的附加参数传递 缓冲区或变量

还应检查错误,因此您应执行以下操作:

char fuel;
if (scanf_s("%c", &fuel, 1) != 1)  {
    puts("Error from scanf_s");
    return 1;
}

你试过使用调试器吗?像这样的问题很容易用一个解决。在这里很有效。您确定键入的是A而不是“A”或A吗?请注意,这似乎是特定于MS特定扫描功能的。使用scanf工作时,请使用VS 12进行检查。但是,您应该阅读整行内容并在应用程序中解析/处理输入。您必须阅读手册页:与scanf和wscanf不同,scanf_s和wscanf_s要求为c、c、s、s等类型的所有输入参数指定缓冲区大小。。。因此,您缺少一个参数scanf_s%c,&fuel;应为scanf_s%c和fuel,1@GabrielMichaeli我们可以看到,你被要求查看变量的实际值,这似乎是错误的,因为它显然不是你认为应该的。你试过使用调试器吗?像这样的问题很容易用一个解决。在这里很有效。您确定键入的是A而不是“A”或A吗?请注意,这似乎是特定于MS特定扫描功能的。使用scanf工作时,请使用VS 12进行检查。但是,您应该阅读整行内容并在应用程序中解析/处理输入。您必须阅读手册页:与scanf和wscanf不同,scanf_s和wscanf_s要求为c、c、s、s等类型的所有输入参数指定缓冲区大小。。。因此,您缺少一个参数scanf_s%c,&fuel;应为scanf_s%c和fuel,1@GabrielMichaeli我们可以看到,你被要求查看变量的实际值,这似乎是不正常的,因为它显然不是你认为应该是的。