OJFord
OJFord

Reputation: 11140

Program ends with exit code 0 immediately - but it shouldn't get there without input

Build succeeds. If I open the output window, it reads:

Program ended with exit code: 0

But my program is such that this shouldn't be possible, without first having taken user input, done some stuff, and taken another user input, all in int main().

The first thing int main() does is loop through taking input for p, until the input is one (of two) desired options. So there's no way it should be able to exit immediately - it initialises p=0 and doesn't exit a while loop until p is 1 or 2.

Is there some hidden error that has allowed the build to succeed without it actually.. succeeding?

int main(){
    //vars
    while (TRUE){
        //play computer or human?
        while (!(p == 1 || p == 2)) {
            printf("Single player or two player? (1/2): ");
            scanf("%d", &p);
        }

        if (p==1) {
            //play computer
        }

        else {
            //snip
        }

        printf("%s won the game! Play again?", winner);
        scanf("%s", playagain);
        if (strncmp(playagain,"no",2)==0){
            break;
        }
    }
    return 0;
}

Upvotes: 1

Views: 3625

Answers (1)

kmort
kmort

Reputation: 2948

Print out what uppercase TRUE is defined as. I have seen some ambitious but inexperienced folks do weird things with it. You might not even be getting into your main while loop.

printf("TRUE is %d\n",TRUE);

If this is nonzero, then your problem is elsewhere.

Upvotes: 3

Related Questions