2017-01-18 6 views
-1

prompt2関数では、2つのif文があります。まずadderprompt2となります。 'x'または 'X'を押すと、最初の 'if'ステートメントに移動してから、xを押すと終了するはずですが、「Answer is ..」と表示されます。基本的には、 'x'または 'X'を押すと、プログラムが終了する場所に移動します。また、私はmainではないので、どのように私はmainでない関数から終了できますか?より多くのコードが必要な場合は、編集して、私に知らせてください。ステートメントでC論理演算子のプログラムが正しく動作しない

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; 

} 
+2

'if(choice == 'D' || 'd')' - > 'if(choice == 'D' || choice == 'd')' 'else if'と同じです。 – kaylum

+0

ああ!本当にありがとう、私はそれをキャッチしていないと信じられない。 –

答えて

3

if (choice == 'D' || 'd')dがゼロに等しくなく、したがってtrueに評価されます。代わりにif (choice == 'D' || choice == 'd')にする必要があります。

1

例として、次の行を変更する必要があります。if (choice == 'D' || 'd')if (choice == 'D' || choice == 'd')。 各変数の後に変数を書き直す必要があります。または& &声明。