在prompt2
函数中,我有两个if
语句。首先adder
会调用prompt2
。当我按'x'或'X'时,为什么它仍然会转到第一个'if'语句,然后打印出来,“Answer is ..”,即使按下x,它应该终止?基本上我想要它,如果你按'x'或'X',程序就会终止。我也不在main
,所以如何从不是main
的函数中终止它呢?如果您需要更多代码,我可以编辑,请告诉我。
int prompt2(int sum) {
char choice; // what the user decides, continuing or not
printf("\nBefore you continue, take a look at my number guess written down on paper.");
printf("\nPress 'D' to display the answer or 'X' to exit: " );
scanf("%c", &choice );
if ( choice == 'D' || 'd' ) {
printf("\nAnswer is %d", sum );
}
else if ( choice == 'X' || 'x' ) {
exit(0);
}
return 1;
}
int adder( int x, int y ) {
int sum = 0;
sum = x + y;
printf("new sum is %d\n", sum );
printf(" %d\n+%d\n----\n?", x, y ); // output appropriate line breaks and spacing for equation
prompt2(sum); // function that asks them if they want to see the answer
return sum;
}
答案 0 :(得分:3)
在if ( choice == 'D' || 'd' )
语句中,d
不等于零,因此评估为true
。它应该是if ( choice == 'D' || choice == 'd' )
。
答案 1 :(得分:1)
例如,您必须使用if ( choice == 'D' || 'd' )
更改此行:if ( choice == 'D' || choice == 'd' )
。
必须在每个||之后重写变量或者&&言。