c++ - If statement always executing even when the condition is false? -


when gets if statement, no matter 'option' inputted setting if(option == "y" || "y") condition true?

bool userinterface::readinconfimdeletion() const {     string option = "";     cout << "are sure want delete these transactions? y/n ";      cin >> option;      if (option == "y" || "y")     {         return true;     }     else if (option == "n" || "n")     {         return false;     }     else{         readinconfimdeletion();         }  } 

you can't compare multiple conditions this:

if (option == "y" || "y") 

the "y" condition evaluate true if evaluated.

you need this:

if (option == "y" || option== "y") 

it simpler imo uppercase string , perform single comparision, there number of options this: convert string in c++ upper case

so possible solution be:

#include <boost/algorithm/string.hpp> string upperstr = boost::to_upper_copy(option); 

then can do:

if (upperstr == "y") 

Comments

Popular posts from this blog

tcpdump - How to check if server received packet (acknowledged) -