2015-11-04 152 views
0

我已經嘗試了很多次,但我無法弄清楚有什麼問題!請幫我解決這個錯誤。我在這裏做錯了什麼?

#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

+4

檢查的第一行的#include <'stdio.h> – artm

+2

請複製粘貼的編譯器的輸出,使用圖像的文字是很煩人的,脆(我不能像訪問由於一些重定向失敗)。 – unwind

+4

'=='運算符不是一個不重要的'any_of_these()'函數。 'if(oper =='+',' - ','*','/','S')'是錯誤的,你可以**將它重寫爲'if(oper =='+'|| oper ==' - '|| oper =='*')'但是因爲你已經有一個'switch'構造,你可以放棄這個''default'塊。請注意,您的錯誤消息_「錯誤,請確保您輸入...」實際上從未顯示過...... –

回答

4

以下爲,規則 - 運算符此

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

是一樣的,因爲這

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

是一樣的,因爲這

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

是一樣的這

if ('/', 'S') 

是一樣的,因爲這

if ('S') 

不等於0雖然總是 「真正」。

C11 Standard (draft)

6.5.17逗號運算符

[...]

語義

逗號的左操作數運算符被評估爲無效表達式;在它的評估和右操作數的評估之間有一個 序列點。然後評估右邊的 操作數;結果有它的類型和價值。

+0

非常感謝! –

3

首先,謹慎對待你的包容:

#include <`stdio.h> 

應該

#include <stdio.h> 

此外,您不能使用,如果關鍵字你所做的一切:

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

應該是:

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

非常感謝! –

相關問題