Favorite Food

Favorite Food

Write a program that prompts the user to enter their favorite food.

Store this in a std::string and print a message saying, "Your favorite food is [food]."

Hint

You have to include <iostream> and <string>.

Use getline to avoid running into problems getting the name of foods with spaces.

Answer

#include <iostream>
#include <string>

int main() {
    std::string favFood;

    // Prompt user to enter their input
    std::cout << "Enter your favorite food: ";
    std::getline(std::cin, favFood);  // Use getline to read the full line, in case the food name has spaces

    // Output the favorite color
    std::cout << "Your favorite food is " << favFood<< "." << std::endl;
}
Questions