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 odpowiedzi
+ 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.