【问题标题】:What have I done wrong here?我在这里做错了什么?
【发布时间】:2015-11-04 10:26:15
【问题描述】:

我已经尝试了很多次,但我不知道出了什么问题!请帮我解决这个错误。

#include <`stdio.h>

int main(void)
{
  float accum = 0, number = 0;
  char oper;

  printf("\nHello. This is a simple 'printing' calculator. Simply enter the number followed");
  printf(" by the operator that you wish to use. ");
  printf("It is possible to use the standard \noperators ( +, -, *, / ) as well as two extra ");
  printf("operators:\n");
  printf("1) S, which sets the accumulator; and\n");
  printf("2) N, that ends  the calculation (N.B. Must place a zero before N). \n");

  do
  {
    printf("\nPlease enter a number and an operator: ");
    scanf("%f %c", &number, &oper);
    if (number == 0 && oper == 'N')
    {
      printf("Total = %f", accum);
      printf("\nEnd of calculations.");
    }
    else if (oper == '+', '-', '*', '/', 'S')
    {
      switch (oper)
      {
        case 'S':
          accum = number;
          printf("= %f", accum);
          break;
        case '+':
          accum = accum + number;
          printf("= %f", accum);
          break;
        case '-':
          accum = accum - number;
          printf("= %f", accum);
          break;
        case '*':
          accum = accum * number;
          printf("= %f", accum);
          break;
        case '/':
          if (number != 0)
          {
            accum = accum / number;
            printf("= %f", accum);
          }
          else
            printf("Cannot divide by zero.");
          break;
        default:
          printf("Error. Please ensure you enter a correct number and operator.");
          break;
      }
    }
    else
      printf("Error. Please ensure you enter a correct number and operator.");
  }
  while (oper != 'N');
  return 0;
}

当我编译此代码时,我收到以下错误,如此处的快照图像所示。 snapshot of error message

【问题讨论】:

  • 检查第一行#include
  • 请复制粘贴编译器输出,使用图像作为文本非常烦人和脆弱(由于某些重定向失败,我无法访问图像)。
  • == 运算符不是假设的 any_of_these() 函数。 if (oper == '+', '-', '*', '/', 'S') 是错误的,您可以将其重写为 if (oper == '+' || oper == '-' || oper == '*'),但因为您已经有一个 switch 构造,您可能会放弃它以支持 default 块。请注意,您的错误消息 “错误。请确保您输入...” 实际上从未显示...
  • 您可以使用if (strchr("+-*/S", oper)) 代替if (oper == '+', '-', '*', '/', 'S')。或者甚至只是删除整个 if-clause,因为它与 switchdefault 分支相同。
  • 您希望(oper == '+', '-', '*', '/', 'S') 做什么?

标签: c compiler-errors


【解决方案1】:

遵循,-operator this 的规则

if (oper == '+', '-', '*', '/', 'S')

和这个一样

if ('-', '*', '/', 'S')

和这个一样

if ('*', '/', 'S')

和这个一样

if ('/', 'S')

和这个一样

if ('S')

不等于0,但始终为“true”。

来自C11 Standard (draft)

6.5.17 逗号运算符

[...]

语义

2 逗号运算符的左操作数被评估为 void 表达式;有一个 其求值与右操作数求值之间的序列点。那么右边 评估操作数;结果有它的类型和值。

【讨论】:

    【解决方案2】:

    首先,请谨慎对待您的收录:

    #include <`stdio.h> 
    

    应该是

    #include <stdio.h>
    

    此外,您不能像以前那样使用 if 关键字:

    if (oper == '+', '-', '*', '/', 'S')
    

    应该是:

    if (oper == '+' || oper =='-' || oper == '*' || oper == '/' || oper ==  'S')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-03
      • 2013-06-28
      • 2023-01-19
      • 1970-01-01
      • 2020-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多