I am trying to allow only yes or no and exit problem if the answer is either number of other letters. How am i supposed to add it?
#include <stdio.h>
int main(void) {
char answer;
scanf("%c", &answer);
if (answer == 'y') {
printf("You Entered: 'y'\n");
printf("\n");
printf("your turn\n");
printf("\n");
}
else if (answer == 'n') {
printf("You Entered: 'n'");
printf("\n");
printf("other's turn\n");
printf("\n");
}
else (answer != 'y' && answer != 'n'); {
if (answer == '%d') {
printf("You Entered: '%d'\n", answer);
printf("You did not enter 'y' or 'n', program now exiting... \n");
if (answer <= 'A' || answer <='a') {
printf("You Entered: '%c'\n", answer);
printf("You did not enter 'y' or 'n', program now exiting... \n");
}
}
return 0;
}
CodePudding user response:
You already made an if
block, which will check that the condition
answer == 'y'
is met, then after that there is an else if
else if(answer == 'n')
which will execute ONLY if the previous if
statement's condition was not satisfied, AND based on the condition that you set, in this case:
answer == 'n'
Now, if you want to throw an error each and every time the user inputs anything different than those two values, there's no simpler solution than this one: making an else
statement.
The else
statement will execute ONLY if all other previous if
s and else if
s aren't executed.
Your final code could look like this:
#include <stdio.h>
int main(void) {
char answer;
scanf("%c", &answer);
if(answer == 'y') {
printf("You Entered: 'y'\n");
printf("your turn\n\n");
}
else if(answer == 'n') {
printf("You Entered: 'n'\n");
printf("other's turn\n\n");
}
else {
printf("You entered neither 'y' nor 'n', program will now exit...\n");
}
return 0;
}
CodePudding user response:
Your code have two problems:
You are missing a parenthesis at the last of your code and other small errors.
if (answer == '%d')
this comparison is invalid.%d
is a format specifier, so it cannot store any value.
After correcting your code, it should look like this:
#include <stdio.h>
int main(void) {
char answer;
scanf("%c", &answer);
if (answer == 'y') {
printf("You Entered: 'y'\n");
printf("\n");
printf("your turn\n");
printf("\n");
}
else if (answer == 'n') {
printf("You Entered: 'n'");
printf("\n");
printf("other's turn\n");
printf("\n");
}
else if (answer != 'y' && answer != 'n') {
printf("You Entered: '%d'\n", answer);
printf("You did not enter 'y' or 'n', program now exiting... \n");
}
return 0;
}
Hope this helped you out.
CodePudding user response:
A solution to this is to break the check out into a separate function which can read in a character and return true
or false
depending on whether the character read is one of 'y'
or 'n'
.
The return of this can then be checked in main
and acted on accordingly.
In this case a conditional without else
acts as a sentinel that prevents control flow from continuing by returning 1
if get_yes_or_no
returns false
.
#include <stdio.h>
#include <stdbool.h>
bool get_yes_or_no(char *ch) {
if (scanf("%c", ch) == 1 && (*ch == 'y' || *ch == 'n'))
return true;
return false;
}
int main(void) {
char answer;
if (!get_yes_or_no(&answer)) {
printf("You Entered: '%d'\n", answer);
printf("You did not enter 'y' or 'n', program now exiting... \n");
return 1;
}
if (answer == 'y') {
printf("You Entered: 'y'\n");
printf("\n");
printf("your turn\n");
printf("\n");
}
else {
printf("You Entered: 'n'");
printf("\n");
printf("other's turn\n");
printf("\n");
}
return 0;
}