Coding With Flavio
Lesson Summary:
User Input
The first thing we do in this lesson is to take some input from a user through the console. The user inputs some text (a string) and we keep track of this text to use it later. We use a string variable to store the text input by the user. Then we process that string variable in a few ways. To get an input from the console and store it into a string variable, we use the following code:
string myInput = Console.In.ReadLine();
This will allow the user to type some text and once the enter key is pressed, that text will be stored into the string variable named "myInput". Note that we can call this variable anything, such as myInput, foo, or simply input.
Using the input
Once we have a user input stored in a string variable named "myInput", we can use this value in our code. As a simple example, we can simply output it back to the console by using the following code:
Console.Out.WriteLine(myInput);
This will just output the input to the console, unmodified. You can also do something like this:
Console.Out.WriteLine("You typed: " + myInput);
This will write out the text "You typed: " followed by the input (note there is a space between the colon and the rightmost double quote). Notice the use of the plus sign, +. In the previous lesson we used this to add numbers together. When used with two strings, however, it will instead stick them together (this is called a string concatenation. Say my input is "Hello World". The following expression:
"You typed: " + myInput
Will become
"You typed: " + "Hello World"
And the plus sign will turn these 2 strings into one string:
"You typed: Hello World"
As a result, the write line will become
Console.Out.WriteLine("You typed: Hello World");
Turning an input into a number
When using an input as a string (such as to output a message to the console), you don't need to do anything special. However, maybe you want to input a number, such as for a calculator. Console.In.ReadLine() reads ALL inputs as strings; even if you type 123 into the console, it will become the string of text "123". If you try to add two string inputs together, such as "123" + "5", you will not get 128; instead, you'll get "1235", due to string concatenation as described in the previous section. Thus you need to do some extra work if you want your numerical input to be used as a number like an integer.
Lucky for us, there is a simple way to do this. We can use the following code to turn a string into an integer:
int myNum = int.Parse(myInput);
You simply put your input variable (in this case "myInput") inside the parentheses of int.Parse(). Do note that if you put in an input that is not a number, such as "abc", this program will just crash. We will learn how to remedy this several lessons later. After using this code, myNum will be an integer variable holding the numerical value of your string. You can now use this to add numbers together!
Lesson FAQ:
What exactly is "parsing"?
You may have noticed you used int.Parse to turn the string input into an integer value. Parsing is simply the act of extracting some meaning from a string Parsing a string into an int means the string will be interpreted into the number that it represents.
What do you mean when you say "Dynamic"?
You may notice the term "dynamic" thrown around a lot. It can mean many things in the context of programming and computer science. In a general sense, dynamic refers to stuff that happens as the program is running (often called the "runtime"), such as getting input from a user and reacting to it, or making a call to the internet.
This is in contrast to stuff which is defined before the program runs at all, which we refer to as static. I will elaborate more on this distinction in future episodes, as it can get be really confusing to identify the difference this early in our lessons. One simple example of this though is the difference between these two:
string myVal1 = "The value";
string myVal2 = Console.In.ReadLine();
The initial value of myVal1 is decided when the code is compiled, before it is run. In contrast, the initial value of myVal2 is decided as the code is run. Note that we can still change the value of both of these during runtime, regardless of how they are initialized.
By the way, we usually use the term "hardcoding" to refer to the use of a value directly in code, such as "The value" above.