[Solved] C++ Functions run no matter input


Your if statements have more entries then a teenage girls phone. 🙂

You should invest in some structures, containers and loops.
For example:

const static std::string question_words[] =  
{"am", "are", "can", "did", "could", "do", "does"};
const static unsigned int word_quantity =
    sizeof(question_words) / sizeof(question_words[0]);

//...
bool is_question = false;
for (unsigned int i = 0; i < word_quantity; ++i)
{
  if (text == question_word[i])
  {
    is_question = true;
    break;
  }
}

You can put this logic into a function, and pass it the word array and the word. That way, you can call the function for the different word containers and not write extra code. Your code would like like:

  bool is_question = false;
  is_question = Is_Word_In_Array(question_words, word_quantity, text);
  if (is_question)
  {
    cout << "sentence is a question\n";
  }
  if (Is_Word_In_Array(greeting_words, greeting_word_quantity, text))
  {
    cout << "sentence has a greeting.\n";
  }

If you sort the arrays, you can use existing search functions like std::binary_search to find your word.

solved C++ Functions run no matter input