Reputation: 9
I've got a weird issue. Tried to use a if-statement and a do-while loop to validate my condition in case of the char variable is not equal to 'c' and 'f' but when I chose 'c' or 'C' or 'f' or 'F' still displaying "Wrong Choice" Following the part that does not work. I tracked printing the variable (as you can see the printf()
code bellow commented) when I type 'c' I can see that the program accepted 'c' but still displaying as wrong input.
I've called #include<stdio.h>
and #include<ctype.h>
already.
Anyone can help me?
char choice;
do
{
printf("\nChoose between Celsius (c) or for Fahrenheit (f):");
scanf(" %c*%c", &choice);
//printf("\nchoice1: %c", choice);
choice = tolower(choice);
//printf("\nchoice2: %c", choice);
if(choice != 'c' || choice != 'f')
printf("\nWrong choice!!");
}while(choice!='c' || choice!='f');
Upvotes: 1
Views: 6741
Reputation: 1181
Correction:
if(choice != 'c' && choice != 'f')
printf("\nWrong choice!!");
Upvotes: 0
Reputation: 122403
if(choice != 'c' || choice != 'f')
will result true for all choice
, it should be
if(choice != 'c' && choice != 'f')
Upvotes: 3
Reputation: 182779
Whatever the choice is, it will be unequal to c
or unequal to f
. If it's equal to c
, it's unequal to f
. If it's equal to f
, it's unequal to c
. So both of your conditions will always be true.
You want choice != 'c' && choice != 'f'
.
Upvotes: 3