if I have:
const string food[] = {"Burgers", "3", "Fries", "Milkshake"}
string word;
cin >> word;
How can I compare word with the correct food? or rather, if the user inputs "Fries", how can I compare that with the string array?
With the find algorithm from <algorithm>:
auto found = std::find(std::begin(food), std::end(food), word);
if (found == std::end(food)) {
// not found
} else {
// found points to the array element
}
or with a loop:
for (const auto &item : food) {
if (item == word) {
// found it
}
}
although, if you need to do this a lot, it might be better to store the items in a data structure designed for quick searches: std::set or std::unordered_set.
std::mapto take advantage of the look-up functionality.std::setseems more appropriate.