Error C2050 开关表达式不完整

Error C2050 switch expression not integral

所以我正在努力处理这个特定的代码片段:

    scanf("%f", &x, &y);
    scanf("%c", &operator);

    switch (x)
    {
    case ('p') : x = lastNum;
        break;

    case ('q') : return(0);

    default: break;

并且 Visual Studio 正因此抛出错误 2050。现在,我明白了为什么会这样,因为在这种情况下 x 是一个浮点数,我正在尝试考虑非数字输入,但是有没有什么方法可以解决这个问题而不诉诸某种形式的魔法?

编辑

这是我的全部代码,仍然有问题,但我认为我的代码片段有点没用。

#pragma warning(disable: 4996)
#include <stdio.h>

int main()
{
float x, y, lastNum;
char operator;

while (1)
{
    scanf("%f %c %f", &x, &operator, &y);

    switch (operator)
    {
    case('+') : lastNum = x + y;
        printf("\nANS = ", "%f", x + y);
        break;

    case('-') : lastNum = x - y;
        printf("\nANS = ", "%f", x - y);

        break;

    case('*') : lastNum = x * y;
        printf("\nANS = ", "%f", x * y);

        break;

    case('/') : lastNum = x / y;
        printf("\nANS = ", "%f", x / y);

        break;

    }


}



}

当您在变量 x 中存储浮点值时,您不能使用 char。 如果要将 char 值存储在 x 中,请将 scanf 语句更改为此 扫描(%c,&x) 否则,您将浮点值存储在 x 中,并在 CASE 语句中写入浮点值。

首先,在你的代码中,

 scanf("%f", &x, &y);

没有任何意义,因为您给出一个格式说明符并传递两个参数。

就是说,关于错误消息,引用 C11 标准,章节 §6.8.4.2

The controlling expression of a switch statement shall have integer type.

因此,您不能使用 float 值作为 switch 语句的控制表达式。但是,从您下面的代码片段中,我们可以看到,您正在使用 int 值作为 case 语句,因此您肯定可以将控制元素设为 int.

您应该将 x 设置为 char 或 int 变量。您不能在开关中使用浮点数并与字符进行比较。此外,您应该:

scanf("%c %f", &x, &y);

因为您在那里收到两个值。

在这种情况下您可以使用不同的变量而不是 x:

case ('p') : x = lastNum;

如果您想保存一个数字...而不是使用 x 来接收这个 lastNum,您可以创建一个不同的变量并仅将 x 用于这些情况。

因此,根据您对另一个答案的评论,我想我理解您要完成的任务 - 用户可以选择输入浮点值 非-数值,例如 'p'(使用以前的值)和 'q'(退出),您正试图弄清楚如何考虑这两者。

使用 scanf( "%f", &x ); 无效,因为它不接受 'p''q' 作为有效输入;匹配失败会return0,x不会更新。

相反,您将不得不以文本形式读取输入,测试第一个字符是 'p''q' 还是数字,然后从那里开始处理.假设我已经正确理解了您的意图,您可能想要执行以下操作:

#include <stdlib.h> // for strtod

char input[SIZE]; // where SIZE is large enough to account for your largest
                  // possible floating point input, plus an optional sign,
                  // plus a newline, plus the 0 terminator.

/**
 * Read input as text
 */
if ( fgets( input, sizeof input, stdin ) ) 
{
  /**
   * Check for a newline character - if it isn't present, then the user
   * typed in a longer input than we're capable of handling.
   */
  char *newline = strchr( input, '\n' );
  if ( !newline )
  {
    fprintf( stderr, "input too long, try again\n" );
    return 0;
  }

  /**
   * Remove the trailing newline
   */
  *newline = 0;

  /**
   * Skip over any leading whitespace
   */
  char *p = input;
  while ( isspace( *p ) ) 
    p++;                    

  /**
   * If the first non-whitespace is a digit or a sign, process as 
   * a numeric input
   */ 
  if ( isdigit( *p ) || *p == '+' || *p == '-' )
  {
    /**
     * Convert input to a floating point value, save to x
     */
    char *chk;
    x = (float) strtod( p, &chk ); // I'm assuming x is declared float

    /**
     * Make sure there wasn't any garbage at the end of the floating-point
     * input.
     */
    if ( !isspace( *chk ) && *chk != 0 )
    {
      fprintf( "%s is not a valid floating-point input\n", p );
      return 0;
    }
  }
  else if ( tolower(*p) == 'p' )
  {
    x = lastVal;
  }
  else if ( tolower(*p) == 'q' )
  {
    return 0;
  }
  else
  {
    fprintf( stderr, "Don't know what to do with %s\n", p );
    return 0;
  }
}
/**
 * process x
 */

编辑

如果您不被允许使用 stdlib.h 中的例程,那么您应该做的第一件事就是责备您的老师,因为他让这项作业变得比需要的更难。

好吧,也许不是——这可能会影响你的成绩。

您可以使用 sscanf 而不是 strtod 来进行转换,但错误检查不太直接:

if ( isdigit( *p ) || *p == '+' || *p == '-' )
{
  char chk;
  int r = sscanf( p, "%f%c", &x, &chk );
  if ( r < 1 || ( r == 2 && !isspace( chk ) && chk != 0 ) )
  {
    // bad input, handle like above
  }
}

所有 *scanf 函数都会 return 成功转换和赋值的次数。在正常情况下,我们期望读取 2 项 - 浮点数和尾随换行符。如果我们读取 2 项并且 chk 而不是 换行符或 0,那么我们在浮点数之后有尾随错误输入。如果我们读取的项目少于一项,那么我们要么匹配失败(输入不是有效的浮点数),要么在读取过程中出错。由于我们在到达这里之前已经检查了前导数字,所以我们 不应该 在这一点上得到完全匹配失败,但为了完整起见,我包括了检查。

编辑编辑

好吧,出于多种原因,您肯定不想将所有这些都粘贴到您的代码中。当输入可以采用多种形式之一时,我主要是想展示相当可靠的方法来检查您的输入。

从所有这些中获取的主要内容如下:

  1. 如果输入行的第一行可以是文本或数值,请先将其读取并存储为文本字符串。您可以使用 fgets()(它将读取 everything 直到换行符,或者直到您填满目标缓冲区,以先到者为准)或 scanf( "%s", ... )(它将读取下一个空白字符);

  2. 检查输入的第一个非空白字符 - 如果是符号或数字,则需要将该输入转换为浮点值。

  3. 读取您的运算符和第二个浮点输入。

这是另一种不太可靠的方法(这将替换上面更新的代码片段中的 scanf( "%f %c %f", &x, &operator, &y );):

char xstr[SIZE]; // large enough to hold a single floating-point value as
                 // text, plus sign, plus terminator

/**
 * Read the first input up to the next whitespace character
 */
scanf( "%s", xstr );
if ( xstr[0] == 'p' )
{
  x = lastVal;
}
else if ( xstr[0] == 'q' )
{
  return 0;
}
else
{
 /**
  * Convert x to a float value
  */
 sscanf( xstr, "%f", &x );

 /**
  * Read the remainder of the input
  */
 scanf( " %c %f", &operator, &y );
}

然后打开运算符(假设您的输入可以采用 12.3 + 4.56 p + 7.89 的形式)。这保证不会使用 stdio.h 中没有的任何内容。

我意识到使用 fgets 将读取 整行 输入,包括您的运算符和第二个操作数,因此我将上面的内容更改为仅读取第一个输入。

应该检查scanf(和sscanf)的return值,以确保您确实阅读了之前的意思加工;我把它从上面遗漏了,以使代码更清晰一些。

我想让你明白的一件事是,C 中的交互式 I/O 是一个 令人头疼的问题