Ask for Favorite Food and Print a Sentence in CSharp

Ask for Favorite Food and Print a Sentence

Introduction

One of the most fundamental skills in programming is learning how to interact with users by accepting input and displaying output. Input allows the user to provide data to the program, while output lets the program respond or give feedback. In C#, the Console.ReadLine() method is the most common way to receive input from the keyboard.

In this lesson, we will create a fun and simple program where the user will enter their favorite food, and the program will respond with a sentence using their input. This exercise will help you understand how Console.ReadLine() works, how to store user-provided information in variables, and how to display dynamic messages back to the user.

While this task seems simple, it introduces essential programming concepts such as:

  • Variables and data types
  • User interaction through input and output
  • String concatenation and interpolation

By mastering these concepts, you’ll be ready to create more interactive and meaningful console applications.


Learning Objectives

Introduction to Objectives:
The goal of this lesson is to help you develop a solid foundation in handling input and output operations. Using Outcome-Based Education (OBE), these objectives are categorized into four levels to guide your learning journey.

  1. Understand – Explain how Console.ReadLine() works and why user input is important in programming.
  2. Learn – Identify and demonstrate proper use of variables and string handling when working with user input.
  3. Practice – Write a program that asks for the user’s favorite food and displays a personalized sentence using their response.
  4. Apply – Enhance the program by adding validation and more interactive features.

Step-by-Step Tutorial

Step 1: Set Up Your Project

  1. Open Visual Studio or Visual Studio Code.
  2. Create a new Console Application project.
  3. Name your project: FavoriteFoodProgram.

Step 2: Writing the Source Code

Below is the beginner-friendly C# source code for this lesson:

using System;

namespace FavoriteFoodProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            // Display a welcome message
            Console.WriteLine("Welcome to the Favorite Food Program!");

            // Ask the user to input their favorite food
            Console.Write("Please enter your favorite food: ");
            string favoriteFood = Console.ReadLine(); // Capture input from the user

            // Display a message using the user's input
            Console.WriteLine("Wow! " + favoriteFood + " sounds delicious. Enjoy your meal!");

            // End program message
            Console.WriteLine("Press Enter to exit...");
            Console.ReadLine();
        }
    }
}

Step 3: Example Output

Input:

Welcome to the Favorite Food Program!
Please enter your favorite food: Pizza

Output:

Wow! Pizza sounds delicious. Enjoy your meal!
Press Enter to exit...

Code Explanation

  • Console.WriteLine() – Prints a message or output to the screen.
  • Console.ReadLine() – Captures the user’s keyboard input as a string.
  • Variable favoriteFood – Stores the user’s input for later use.
  • String concatenation (+) – Combines text and variables into one message.

This simple program introduces string handling and how to make programs interactive and dynamic.


Summary

In this lesson, you learned how to create a simple program that interacts with the user by asking for their favorite food and printing a personalized response. We explored how to use Console.ReadLine() to capture input, how to store data in variables, and how to display meaningful output. These foundational skills are essential for building interactive applications and serve as the basis for more complex programming tasks.


Multiple-Choice Questions (Bloom’s Taxonomy)

  1. Remembering:
    Which method is used to capture user input in C#?
    A. Console.WriteLine()
    B. Console.Input()
    C. Console.ReadLine()
    D. Console.Scan()

Answer: C


  1. Understanding:
    Why do we store the value of Console.ReadLine() in a variable like favoriteFood?
    A. To delete it later
    B. To use it multiple times in the program
    C. To convert it automatically to a number
    D. It is optional and not necessary

Answer: B


  1. Applying:
    If the user types “Burger,” what will the program output?
    A. Burger sounds delicious.
    B. Wow! Burger sounds delicious. Enjoy your meal!
    C. Favorite food is Burger.
    D. Burger is stored successfully.

Answer: B


  1. Analyzing:
    What would happen if you do not use Console.ReadLine() at all in the program?
    A. The program will not run.
    B. The program will not accept user input.
    C. The output will show random data.
    D. It will automatically guess the input.

Answer: B


  1. Evaluating:
    Which is the best way to improve the program’s interaction with the user?
    A. Add input validation and handle empty responses.
    B. Remove all Console.WriteLine() statements.
    C. Limit the user to specific food options only.
    D. Ignore invalid or blank inputs.

Answer: A


Exercises, Assessment, and Lab Exam

Introduction

Practicing beyond the provided example will help you develop problem-solving skills and strengthen your understanding of user input and output handling. The following tasks gradually increase in difficulty to ensure you gain confidence and mastery of the topic.


Exercises

  1. Modify the program to:
    • Ask the user for two favorite foods and print a combined message.
    • Example: “Pizza and Pasta are great choices!”
  2. Update the program to:
    • Display a thank-you message that includes both the current date and time.
  3. Use string interpolation instead of concatenation:
    Console.WriteLine($"Wow! {favoriteFood} sounds delicious.");
    

Assessment

Create a console program that:

  1. Asks for the user’s name and favorite food.
  2. Prints a message like:
    “Hello Sarah! It’s great to know that you love Sushi.”
  3. Add a validation step to ensure the user does not leave the input blank.
  4. If the input is blank, display an error message and ask again.

Assessment Criteria:

  • Correct use of Console.ReadLine() and string variables.
  • Implementation of validation logic.
  • Clear and professional output formatting.

Lab Exam

Build a Simple Food Ordering Console Application:

  1. Ask for the customer’s name.
  2. Provide a list of 3 food items with numbers (e.g., 1. Pizza, 2. Burger, 3. Salad).
  3. Allow the user to select their order by entering a number.
  4. Display a confirmation message showing the user’s choice.
  5. Include basic input validation to handle incorrect selections.

Additional Requirements:

  • Use comments to explain your code.
  • Implement at least one if-else or switch statement.
  • Ensure the program handles errors gracefully.

You may visit our Facebook page for more information, inquiries, and comments. Please subscribe also to our YouTube Channel to receive free capstone projects resources and computer programming tutorials.

Hire our team to do the project.

, , , , , , , , , ,

Post navigation