Hi all,
I am currently trying to learn C and would appreciate any help...
Can anyone please explain why the following code will not work?
The if statement if (yn!='y' || yn!='n') always becomes true!
Code:
#include<stdio.h>
main()
{
char yn;
while(yn!='e') {
printf("enter y/n? ");
scanf(" %c", &yn);
if (yn=='e') {
printf("goodbye\n");
break;
}
if (yn!='y' || yn!='n') {
printf("error, enter y or n!\n");
}
}
return 0;
}
I did manage to replicate the code with nested if's but after much reading it's better to write more concise code. Correct me if I am wrong?
Code:
#include<stdio.h>
main()
{
char yn;
while(yn!='e') {
printf("enter y/n? ");
scanf(" %c", &yn);
if (yn=='e') {
printf("goodbye\n");
break;
}
if (yn!='y') {
if (yn!='n') {
printf("error, enter y or n!\n");
}
}
}
return 0;
}