-6

I understand that else statements should not have a semicolon at the end of the if statement.

The objective is to make this if/else statement work, so that if the answer is not Y or y, then to print the else statement. My question is:

  • What exactly needs to be done to correct this?

Here's the code:

if (answer=='Y'|| 'y')
scanf(" c", &answer);
printf("\n Great! Keep listening to music. I'm sure your mood will improve.");
else printf("\n Try listening to some music that you enjoy!");
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    You should post code in the question, not post an image of code – M.M Feb 05 '17 at 03:20
  • You really didn't search very hard. http://stackoverflow.com/questions/21441560/c-language-if-with-no-else-using-braces-fails – Rich Feb 05 '17 at 03:24
  • `if (answer=='Y'|| 'y')` won’t work; see https://stackoverflow.com/questions/8781447/can-you-use-3-or-conditions-in-an-if-statement. You also might have meant for the `scanf` format string to be `"%c"`, not `" c"`, and for it to go before the `if`. The syntax is also `if (condition) { … } else { … }`, so make those blocks. For simplicity, never omit curly braces. – Ry- Feb 05 '17 at 03:24
  • Also, this program is to assist people with monitoring their mental health and mood state. It's supposed to make recommendations/suggestions on what to do to improve their mood if it is low (listen to music, call a friend; call a hotline if suicidal, etc). I – Aaron Cortez Feb 05 '17 at 03:26
  • The syntax for if/else in C/C++ is that you can do away with the brace after the `if` and `else` if the the following code is just one line. in your case. `if (answer=='Y'|| 'y'){...}else ...` – Abass Sesay Feb 05 '17 at 03:26
  • @AbassSesay: Python doesn’t have braces and it doesn’t have to do with anything in this question, either. – Ry- Feb 05 '17 at 03:26

1 Answers1

1

The objective is to make this if/else statement work,

You are missing the curly braces in your code.

scanf(" %c", &answer);
 if (answer=='Y'|| answer == 'y') {
     printf("\nGreat! Keep listening to music. I'm sure your mood will improve.\n");
 }
 else {
     printf("\nTry listening to some music that you enjoy!\n");
 }
VHS
  • 9,534
  • 3
  • 19
  • 43