Question

New to C. Simply put I just don't see why this wont work. All I want it to do is just keep asking you if you want to play until your input is 'n'. I keep getting (error C2143: syntax error : missing ';' before 'break')

#include <stdio.h>
int main(void)
{
    char answer;
    printf("Would you like to play? Enter Y or N: ");
    while (scanf_s(" %c", &answer))
    {
        if (answer == 'y')
            printf("Answer is %c\n", answer);
        printf("Success!\n");
        printf("Do you want to play again? Y or N: ");
        else (answer == 'n')
        break;
    }
    printf("Goodbye\n");
    return 0;
}
Was it helpful?

Solution

if and else have to be next to each other... you need to have curly brackets in there as such:

    if (answer == 'y') 
    {
        printf("Answer is %c\n", answer);
        printf("Success!\n");
        printf("Do you want to play again? Y or N: ");
    }
    else
        break;

to do more than one statement in an if block.

If you want to check more than one condition, don't use an if/else but a if/else if like such:

    if (answer == 'y') 
    {
        printf("Answer is %c\n", answer);
        printf("Success!\n");
        printf("Do you want to play again? Y or N: ");
    }
    else if(answer == 'n')
        break;

OTHER TIPS

You will need to rewrite your if-else using brackets:

if (answer == 'y') {
   printf("Answer is %c\n", answer);
   printf("Success!\n");
   printf("Do you want to play again? Y or N: ");
} else if (answer == 'n') {
   break;
}

You can´t write an else with a condition.
else will be executed if the if wasn´t executed, without a second condition check.
If you want to have something to executed
if the if was not executed AND a contition is true, use else if.

You have to write

else if (answer == 'n')

Besides @Mike's answer to add the needed {} to enclose the codes in the if's code scope, you also need to change else to elseif.

So the code should be like this:

if (answer == 'y') 
{
    printf("Answer is %c\n", answer);
    printf("Success!\n");
    printf("Do you want to play again? Y or N: ");
}
else if (answer == 'n')
    break;

P.S.: One minor suggestion, based on the guideline messages showing to user ("Would you like to play? Enter Y or N: "), you probably need also to handle uppercase inputs, i.e. 'Y' and 'N'. You can simply do like this:

if (answer=='y' || answer=='Y') 
{
    // ...
}
else if (answer=='n' || answer=='N')
    break;

There is no need to duplicate glibc rpmatch() function, that does good job interpreting yes/no answers.

#include <stdio.h>
#include <string.h>
#include <stdlib.h> 
int main(int argc, char **arg)
{
        int r;
        char *answer = NULL;
        size_t len = 0;
        while (1) {
                printf("Would you like to play? Enter Y or N: ");
                getline(&answer, &len, stdin);
                r = rpmatch(answer);
                if (r < 0)
                        printf("????\n");
                else if (r == 0)
                        break;
        }
        free(answer);
        return(0);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top