基本的に、アキュムレータを実装する単純なコマンド ライン計算機を作成しています。このコードは論理的に正しく構造化されているように感じますが、print ステートメントの無限ループに入る前に約 3 秒間フリーズする理由がわかりません。どんな助けでも大歓迎です。
void mycalc() {
printf("Begin Calculations\n\n");
printf("Initialize your Accumulator with data of the form \"number\" \"S\" which\
sets the Accumulator to the value of your number\n");
/* Initialize Variables */
float accumulator, num;
char op;
/* Ask for input */
scanf("%f %c\n", &num, &op);
while (op != 'E') {
if(op == 'S' || op == 's'){
accumulator = num;
printf("Value in the Accumulator = %f\n", accumulator);
} else if(op == '+'){
accumulator = accumulator + num;
printf("Value in the Accumulator = %f\n", accumulator);
} else if(op == '*'){
accumulator = accumulator * num;
printf("Value in the Accumulator = %f\n", accumulator);
} else if(op == '/'){
if (num == 0) {
printf("Can not divide by 0.\n");
} else {
accumulator = accumulator / num;
printf("Value in the Accumulator = %f\n", accumulator);
}
} else if(op == '-'){
accumulator = accumulator - num;
printf("Value in the Accumulator = %f\n", accumulator);
} else if(op == 'E' || op == 'e'){
printf("Value in the Accumulator = %f\n", accumulator);
break;
} else {
printf("Unknown operator. \n");
}
scanf("%f %c\n", &num, &op);
}
}
代わりに while(1) 手法を使用する方がよいでしょうか? どんな助けでも大歓迎です!ありがとう!