janac

Determine if a word is on the list

Nov 10th, 2021 (edited)
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. // Determine if a word is on the list.
  7.  
  8. // This function returns true if the
  9. // strings match. Otherwise false.
  10. bool is_on_list(string word, vector<string>& word_list)
  11. {
  12.     // Check each item on the list.
  13.     for (string current_word : word_list)
  14.     {
  15.         // If the word matches,
  16.         if (current_word.compare(word) == 0)
  17.         {
  18.             // The word is on the list.
  19.             return true;
  20.         }
  21.     }
  22.  
  23.     // Otherwise, the word does not match.
  24.     return false;
  25. }
  26.  
  27. int main()
  28. {
  29.     string input; // This is the input.
  30.     vector<string> list_of_words = {"cat", "dog", "bird"};
  31.  
  32.     cout << "Enter an animal (lowercase):" << endl;
  33.     cin >> input; // Get the user's input.
  34.  
  35.     // If the word is on the list.
  36.     if (is_on_list(input, list_of_words))
  37.     {
  38.         cout << input << " is on the list." << endl;
  39.     }
  40.     else // Otherwise if the word is not on the list,
  41.     {
  42.         cout << input << " isn't on the list." << endl;
  43.     }
  44.  
  45.     return 0;
  46. }
Add Comment
Please, Sign In to add comment