2016-11-19 1 views
0

私のコード: 私は初心者ですので、私には簡単に行きます switchステートメントは、次のコードで複数のステートメントを実行します。私は問題を見つけることができないようです。すべてが正しいようです。 出力イメージと私のコードをチェックアウトすると、私は助けが必要です。スイッチステートメント複数のケースが実行されました - Cプログラミング

//write a program that acts as a simple "printing" calculator 
//enter expressions in the format "operator number" 
//include operators to 'set accumulator' and 'end execution' 
#include<stdio.h> 
int main() 
{ 
float number, accumulator; 
char operator; 

printf("Begin Calculations: (Enter 'S' operator to set the accumulator and 'E' operator to end execution)\n"); 
printf("-------------------\n\n"); 

while(operator != 'E') 
{ 
    scanf("%c%f", &operator, &number); 
    switch(operator) 
    { 
    case 'S': 
    accumulator = number; 
    break; 
    case '+': 
    accumulator += number; 
    break; 
    case '-': 
    accumulator -= number; 
    break; 
    case '*': 
    accumulator *= number; 
    break; 
    case '/': 
    accumulator /= number; 
    break; 
    case 'E': 
    printf("End of Calculations.\n"); 
    break; 
    default: 
    printf("Enter proper Expression.\n"); 
    break; 
    } 
    printf("= %f\n", accumulator); 
} 

return 0; 
} 

click for output:

+0

テキストの画像を投稿しないでください! **常に**あなたのプログラムに関連するエラーが発生する可能性のある関数の結果をチェックしてください。 – Olaf

+0

'scanf'がENTERを' operator' *に読み込んでいます*(ループ内で浮動小数点数を 'number' * **に読み込めません!!)** – pmg

答えて

1

scanf正常あなたの形式指定子から割り当てられた変数の数を示す整数を返します。

戻り値scanfを確認していませんが、値を割り当てる予定の変数を使用しています。これは常に間違いです。

scanfはおそらく失敗しており、ループは以前の値のoperatorで継続しています。

この場合、scanfが2を返すことを確認する必要があります。それ以外の場合は、有効な入力が得られず、変数も変更されません。

関連する問題