0

Help! else if (x != "") did not work as I wanted

else if((Ttype != "c") || (Ttype != "C") || (Ttype != "f") || (Ttype != "F")){ cout << "Wrong input..."; getch(); goto temp; } Is there is something wrong the way I use the if not? because when I run it, it turns out to be running the condition even tho the input is "C".

7th Mar 2018, 2:47 PM
Faza
Faza - avatar
8 ответов
+ 3
|| doesn’t work because it’s based on OR logic, if any of the conditions are true, the if statement will executed. So if you input F, it’s still going to say wrong input because it’s not equal to C. Where as && is based on AND logic, all of the conditions need to be true for the if statement to be executed.
7th Mar 2018, 3:17 PM
aklex
aklex - avatar
+ 2
So you are trying to check if it’s it’s C or F? If that’s the case, you should be using &&, because || will fail
7th Mar 2018, 3:07 PM
aklex
aklex - avatar
+ 1
@aklex Thanks you so much! It worked! Yeah I wanted to check if its not f/c it will say wrong input. Can you explain why I should use && instead of || ?
7th Mar 2018, 3:11 PM
Faza
Faza - avatar
0
full code temp: system("CLS"); string Ttype; float degree; cout << "What is the temperature type? (C/F) (/goback)\n"; cin >> Ttype; if ((Ttype == "goback") || (Ttype == "/goback")){ goto mainmenu; } else if((Ttype != "c") || (Ttype != "C") || (Ttype != "f") || (Ttype != "F")){ cout << "Wrong input..."; getch(); goto temp; } cout << "\nWhat is the Temperature degree? (Number/s)\n"; cin >> degree; if((Ttype == "c") || (Ttype == "C")){ degree = degree * (9/5) + 32; cout << "\nIt's " << degree << " degree Fahrenheit"; } else if((Ttype == "f") || (Ttype == "F")){ degree = (degree - 32) * 5/9; cout << "\nIt's " << degree << " degree Celsius"; } else{ cout << "\nWrong input..."; getch(); goto temp; } cout << "\n\nPress any to continue..."; getch(); goto temp; return 0; }
7th Mar 2018, 2:36 PM
Faza
Faza - avatar
0
You are comparing pointer addresses, which is not what you want. You need to use ‘C’ instead of “C” Edit: Nvm, I did not see the rest of your code!
7th Mar 2018, 2:47 PM
aklex
aklex - avatar
0
nope, doesn't work. its a string tho, not a char.
7th Mar 2018, 2:50 PM
Faza
Faza - avatar
0
put ur code in playground and send it's link
7th Mar 2018, 3:05 PM
Farshaad Heydari
Farshaad Heydari - avatar
0
Getch, so that the program will wait for a key to continue the code. the goto is for looping.
7th Mar 2018, 4:36 PM
Faza
Faza - avatar