Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- using namespace std;
- // Determine if a word is on the list.
- // This function returns true if the
- // strings match. Otherwise false.
- bool is_on_list(string word, vector<string>& word_list)
- {
- // Check each item on the list.
- for (string current_word : word_list)
- {
- // If the word matches,
- if (current_word.compare(word) == 0)
- {
- // The word is on the list.
- return true;
- }
- }
- // Otherwise, the word does not match.
- return false;
- }
- int main()
- {
- string input; // This is the input.
- vector<string> list_of_words = {"cat", "dog", "bird"};
- cout << "Enter an animal (lowercase):" << endl;
- cin >> input; // Get the user's input.
- // If the word is on the list.
- if (is_on_list(input, list_of_words))
- {
- cout << input << " is on the list." << endl;
- }
- else // Otherwise if the word is not on the list,
- {
- cout << input << " isn't on the list." << endl;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment