0

How do I add an else statement that'll work with both if statements?

If the user input is "yes" then the code doesn't work well, The Output Becomes: Then move to the Right, Dummy. :*Please Enter the Correct Data. #include <stdio.h> #include <stdlib.h> //Playing a Game of Checkers.. int main() { char user_input[4]; printf("Are you Smart? [yes or no]\n"); scanf("%3s", user_input); if (strcmp ("yes", user_input) == 0) { printf("Then Go left smarty-pants :*"); } if (strcmp ("no", user_input) == 0) { printf("Then play to the Right dummy"); } else { printf("Please Enter the Correct Data"); } return (0); }

11th Jul 2021, 11:16 AM
Mwamba
1 Réponse
+ 2
See how you have 2 if-statements and 1 else statement? Make it: if-statement, else-if-statement, else-statement. (literally just add the word "else" before the second if-statement) The second if-statement in your code has an else statement directly below it. And that else statement only applies to the 2nd if-statement. It prints the extra message when "yes" is entered because the first if statement has no else statement to challenge it. Each if-statement can only have one else-statement, but as many else-if-statements that it needs in between.
11th Jul 2021, 11:38 AM
Slick
Slick - avatar