0

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;
}

6 Answers 6

2

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;
Sign up to request clarification or add additional context in comments.

6 Comments

When I try it I still get 2 errors. (error c2043: illegal break) and (c2065: 'answer': undeclared)
Nevermind those two errors I did something stupid.. Ok it works yet it wont run whats in the loop its just outputs the "Goodbye part" #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 if (answer == 'n') break; } printf("Goodbye\n"); return 0; }
@user3365930 - it should work fine... Are you using lower case y and n in your input? (you're only checking for lower case). You can try replacing scanf_s with scanf (I only recommend that because I don't know what scanf_s is supposed to return.
Mike!!! Thank you so much you are right. The book I am using uses scanf() yet VS 2013 gives a error for not using scanf_s(). After looking it up I found out that you can disable the error and make it work by adding "#define _CRT_SECURE_NO_WARNINGS" to you library. Off topic do you know why they want you to use scanf_s() so badly? When it just doesn't seem to work?
@user3365930 - scanf() does have it's problems... mostly people don't read the manual and use it wrong. Microsoft has a habit of "updating" standard functions to make them "safer"... frankly IMO they are just adding confusion. You'd have to poke around the MSDN site to find documentation on scanf_s and what exactly they think they're fixing with it.
|
0

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;
}

Comments

0

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.

Comments

0

You have to write

else if (answer == 'n')

Comments

0

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;

1 Comment

Oh c seems to be smart enough to know that char 'Y' and char "y" are the same thing being implied. Though thats useful to keep in mind say if you didn't want Y to = a true value. Thanks.
0

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.