DEV Community

Cover image for How to read user inputs from a console
Carl-Hugo Marcotte
Carl-Hugo Marcotte

Posted on • Originally published at forevolve.com

How to read user inputs from a console

In this article, we explore how to read user inputs from the console.
This article is the foundation of more dynamic notions enabling our programs to change based on user interactions and react to them.
We also learn how to change the title of the console and how to delete its content.

This article is part of a learn programming series where you need no prior knowledge of programming.
If you want to learn how to program and want to learn it using .NET/C#, this is the right place.
I suggest reading the whole series in order, starting with Creating your first .NET/C# program, but that's not mandatory.

User input

So far, we used the Console.Write and Console.WriteLine methods to write to the console.
We also created variables and constants to hold and reuse some text.
However, we don't know how to interact with the user yet.

There are multiple types of applications, like web apps, mobile apps, and Windows apps.
In our case, we will continue to use console applications because they are the simplest.
We don't need to bother with complex user interfaces, animation, or interaction, and we can focus on learning to program.

Console apps may feel less exciting, but that allows us to focus on only one subject at a time.
Remember that every piece of knowledge that you are acquiring is like a new LEGO® block that you'll be able to piece with the others later.
Moreover, what you are learning in this series is reusable in most, if not all, other types of apps.

The Console class offers three methods to read user inputs, Read, ReadKey, and ReadLine.
We can use the first two for more complex scenarios.
The third one is very straightforward and is the method we are focusing on in this article.

As its name implies, Console.ReadLine reads the line entered by the user.
It is simple and gives us the power to accomplish what we need, to learn the basic programming concepts.

Reading a line entered by a user

The Console.ReadLine method returns a string, representing the line written in the console by the user.
Here is an example:

using System;

Console.Write("Please enter a greeting message, then press ENTER: ");
var hello = Console.ReadLine();
Console.WriteLine(hello);
Enter fullscreen mode Exit fullscreen mode

In the preceding code, we write a message to the user, then wait for an input.
The program will block its execution there until the user hits the <ENTER> key.
At this point, it will resume and continue, then write the read line back to the console.

Here is the console content when running the program and entering Hello Amigo!<ENTER>:

Please enter a greeting message, then press ENTER: Hello Amigo!
Hello Amigo!
Enter fullscreen mode Exit fullscreen mode

More info: it is important to note that the new line character (the <ENTER>) is not part of the line (not saved in the hello variable).

Now that we saw an example, let's explore the flow of execution of the program.

Flow of execution

The program flow, or flow of execution, represents the order in which the program executes the instructions (lines of code).
In our case, the code is linear; it starts at the top and ends at the bottom of the Program.cs file.
Nevertheless, the Console.ReadLine() method blocks the program, waiting for a user input, which disturbs the flow.

Here is what happens:

Code sequence

  1. The program writes the question to the console.
  2. The program executes the right-end of the assignation operator (=), the Console.ReadLine() method, which waits for the user to hit the <ENTER> key. > More info: The assignation operator = is always the last to be executed; it has the lowest priority.
  3. The user types Hello Amigo! then hit <ENTER>.
  4. The program then resumes and assigns the user-entered value to the hello variable.
  5. The program writes that input back to the console.

Here is a second way to visualize this flow:

Code sequence graph

In this case, the Console.ReadLine() method manages the bifurcation even if code-wise, the flow is linear.

Note: We will learn ways to control a program's flow in future articles.

Next, it is your turn to try this out.

Exercise

To practice all that we explored so far, including user-inputs, you must write a program that asks the following two questions to the user:

  1. What is your first name?
  2. What is your last name?

Then, the program must greet that user using the following format: Greetings {first name} {last name}!.

Example: Assuming the user entered Carl-Hugo as the first name and Marcotte as the last name, the greeting message would read Greetings Carl-Hugo Marcotte!.

Unfortunately, I was not able to recreate the whole exercise on this platform, so please look at the exercise on the original post on my blog. I'm sorry for the inconvenience.

Bonus information

In this short section, we explore two more manipulations of the console:

  • How to set its title.
  • How to clear what is written in it.

The block of code at the end of this section contains the preceding exercise's solution; please be advised.

Setting a custom console title

If you want to change the console title, you can set the Console.Title property to the string of your choice, like this:

Console.Title = "IntroToDotNet";
Enter fullscreen mode Exit fullscreen mode

Pretty straightforward, isn't it?
Next, let's see how to clear the text from the console.

Clearing the console

One last bit of knowledge here: we can clear the console using the Console.Clear() method.
So instead of the following output:

What is your first name? Carl-Hugo
What is your last name? Marcotte
Greetings Carl-Hugo Marcotte!
Enter fullscreen mode Exit fullscreen mode

We could clear the console between each question to obtain the following flow:

Adding Console.Clear calls

Here is the code to achieve that result:

Program.cs

using System;

Console.Title = "IntroToDotNet"; // Custom title

Console.Write("What is your first name? ");
var firstName = Console.ReadLine();
Console.Clear(); // Clear after the first question

Console.Write("What is your last name? ");
var lastName = Console.ReadLine();
Console.Clear(); // Clear after the second question

Console.Write("Greetings ");
Console.Write(firstName);
Console.Write(" ");
Console.Write(lastName);
Console.WriteLine("!");
Enter fullscreen mode Exit fullscreen mode

And that's it for this article.

Conclusion

In this article, we explored how to read user inputs from the console.
We used the ReadLine method to acquire the value a user wrote before hitting the <ENTER> key.
Interacting with the user is the foundation of the next many articles where we will use this to acquire and manipulate data typed by the user.

We also looked at how to change the title of the terminal Window because why not, right?
Finally, we explored how to clear the text to reset the console to an empty state.
This second interlude can be very handy at crafting a better user experience (UX).

Next step

It is now time to move to the next article: Introduction to string concatenation and interpolation which is coming soon. Stay tuned by following me on dev.to, Twitter, or other places you can find me.
You can look at my blog contact page for more info.

Top comments (0)