c++ - My Else cout displays even if input is correct -


even type pythagoras or probability still displays else output "topic not supported" how fix this?

search = "pythagoras"; pos = sentence.find(search); if (pos != string::npos)                             cout << "pythagoras entry" << endl; search = "probability"; pos = sentence.find(search); if (pos != string::npos)     cout << "probability entry" << endl;  else     cout << "topic not supported" << endl; 

if sentence contains "pythagoras", pass through first if(outputting what's there), , go on "else" check "probability" - don't break execution after found "pythagoras" in sentence, go second if, outputting what's in "topic not found", because "probability" != "pythagoras". if put "probability" in sentence, "topic not found" should not outputted, unless didn't show code you're using.

to fix this, suggest using boolean flag, like

bool flag = false; search = "pythagoras"; pos = sentence.find(search); if (pos != string::npos)                     {     cout << "pythagoras" << endl;     flag = true; } search = "probability"; pos = sentence.find(search); if (pos != string::npos) {                         cout << "probability" << endl;      flag = true; }  if (!flag)     cout << "not supported" << endl; 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -