Reading from Console

Reading from Console

Now that we know the basics about variables, we can now accept input from the user. Use the following line to tell the console to wait until the user types something into the console. Then, the computer takes whatever is typed into the console, and stores it into the variable called input.

var input = Console.ReadLine();

Here’s an example of how to use Console.ReadLine to accept user input:

Console.WriteLine("Type in something:");
var input = Console.ReadLine();
Console.WriteLine("You typed in: " + input);

When the user presses run, the user is greeted with the following program:

alt text height=

In this case, after the user types in Hello World! into the console, the program finishes outputting to the console:

alt text height=

Working Together

Write a program that first prints the following two lines to console:

Welcome!
How can I help you today?

Afterwards, the console waits for user input. After the user types in something and presses Enter, the computer prints out:

You asked: [input]?
I don't know the answer to that! Goodbye!

[input] should be replaced with whatever the user had typed into the console.