我在这做错了什么?

时间:2015-11-04 10:26:16

标签: c compiler-errors

我已经尝试了很多次,但我无法弄清楚出了什么问题!请帮我解决这个错误。

#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

2 个答案:

答案 0 :(得分:4)

遵循, - 运营商的规则

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

与此相同

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

与此相同

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

与此相同

if ('/', 'S')

与此相同

if ('S')

不等于0,但总是&#34; true &#34;。

来自C11 Standard (draft)

  

6.5.17逗号运算符

     

[...]

     

<强>语义

     

2 逗号运算符的左操作数被计算为void表达式;有一个   其评估与右操作数之间的序列点。然后是正确的   操作数被评估;结果有其类型和价值。

答案 1 :(得分:3)

首先,要谨慎包容:

#include <`stdio.h> 

应该是

#include <stdio.h>

此外,您不能像以下那样使用if-keyword:

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

应该是:

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