我試圖寫我的第一個計算器,並發現了一些在線的例子,然後我改變,使他們更容易在流動的條款。然而,當我改變這個流程:簡單的計算器()
#include <stdio.h>
main()
{
char operator;
float num1,num2;
printf("Enter an operator (+, -, *, /): ");
scanf("%c" ,&operator);
printf("Enter first operand: ");
scanf("%f" ,&num1);
printf("Enter second operand: ");
scanf("%f" ,&num2);
switch(operator)
{
case '+':
printf("num1+num2=%.2f\n" ,num1+num2);
break;
case '-':
printf("num1-num2=%.2f\n" ,num1-num2);
break;
case '*':
printf("num1*num2=%.2f\n" ,num1*num2);
break;
case '/':
printf("num1/num2=%.2f\n" ,num1/num2);
break;
default: //of operator is other than +, -, *, /, erros message shown
printf("Error! Invalid operator, this is basic math only.\n");
}
return 0;
}
這樣:
#include <stdio.h>
main()
{
char operator;
float num1,num2;
printf("Enter first operand: ");
scanf("%f" ,&num1);
printf("Enter an operator (+, -, *, /): ");
scanf("%c" ,&operator);
printf("Enter second operand: ");
scanf("%f" ,&num2);
switch(operator)
{
case '+':
printf("num1+num2=%.2f\n" ,num1+num2);
break;
case '-':
printf("num1-num2=%.2f\n" ,num1-num2);
break;
case '*':
printf("num1*num2=%.2f\n" ,num1*num2);
break;
case '/':
printf("num1/num2=%.2f\n" ,num1/num2);
break;
default: //of operator is other than +, -, *, /, erros message shown
printf("Error! Invalid operator, this is basic math only.\n");
}
return 0;
}
根本改變流從:進入運營商,然後輸入第一個數字,然後第二個數字。要:輸入第一個號碼,然後輸入運營商,然後輸入第二個號碼。 我的問題是,當我這樣做時,我看到了Enter運算符,但程序跳過了輸入運算符的選項並要求輸入第一個數字,然後輸入第二個數字。響應是默認開關。
你看過[這個](http://stackoverflow.com/questions/14484431/scanf-getting-skipped?rq=1)的問題嗎? – charmlessCoin