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".
8 Answers
+ 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.
+ 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
+ 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 || ?
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;
}
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!
0
nope, doesn't work. its a string tho, not a char.
0
put ur code in playground and send it's link
0
Getch, so that the program will wait for a key to continue the code. the goto is for looping.