-1

In the prompt2 function I have two if statements. First adder will call prompt2. When I press 'x' or 'X', why does it still go to the first 'if' statement, and then it prints, "Answer is..", even though by pressing x, it should've terminated? Essentially I want it to where if you press 'x' or 'X', the program just terminates. Also I'm not in main either, so how can I terminate it from a function that isn't main? If you need more code, I can edit, just let me know.

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;

}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Shinji-san
  • 971
  • 4
  • 14
  • 31

2 Answers2

4

In the statement if ( choice == 'D' || 'd' ) the d is not equal to zero and therefore evaluates to true. It should be if ( choice == 'D' || choice == 'd' ) instead.

Jenna Sloan
  • 372
  • 8
  • 22
1

As example, you must change this line: if ( choice == 'D' || 'd' ) with if ( choice == 'D' || choice == 'd' ). The variable must be rewritten after each || or && statement.

Pez_93
  • 34
  • 3