當它達到這個if語句時,不管輸入了什麼'option',它總是將if(option ==「y」||「Y」)條件設置爲true?即使條件爲假,語句也總是執行?
bool UserInterface::readInConfimDeletion() const
{
string option = "";
cout << "Are you sure you want to delete these transactions? Y/N ";
cin >> option;
if (option == "y" || "Y")
{
return true;
}
else if (option == "n" || "N")
{
return false;
}
else{
readInConfimDeletion();
}
}
可能的重複[如何比較Java中的字符串?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – EJP 2015-04-01 10:49:58
@EJP這是C++不是java – EdChum 2015-04-01 10:50:47
底線是不要把C++'if'條件當成你說英語。 *如果選項等於'Y'或'y'*在講話時可以理解,但C++不以這種方式工作。 – PaulMcKenzie 2015-04-01 10:52:35