C-SHARP - 2.5 Interact with the user

Hello everyone and welcome to this new video. In this video we will see how to interact with the user. That is, the user will be able to enter data directly. Basically in previous videos, we initialized the variables directly with information in our code. Now the user will enter his data directly and it will be stored in variables. So let's go, I'll show you how to do it? Let's go to Visual studio. We are now on our FDI. And we will create a program that will allow us to interact with the user. So we will first ask the user a question. We make a Console.WriteLine() so we will type cw tab We will display in the output window the following question: you put in parentheses "What is your first name?" Here you see that we just displayed a question in the exit window. Now we will ask the user to enter his name and there is a function that allows us to do this. This is its Console.ReadLine(). This function will allow the user to enter something in the output window. And this function is said to be blocking That is to say, as long as the user does not enter something the program will remain blocked Here it was a small precision. Then another indication, this function only supports strings. That is, everything you are going to type, will be considered as a string, even if you type a number it will be considered as string and not int If you then want to transform it into int to change this value you will then have to convert it to int. This conversion topic will be discussed in the next video. Don't worry. For the moment, let's stay on our interaction with the user. Now, we're going to launch the program to see what it looks like. You see we have our question "What is your first name? "And we can now enter something with our keyboard. For example, we're going to write the name "Ilyass" We make Enter. and you see? Nothing happens. Why? In our program, we simply used the function that allows us to interact with the user but we don't tell him, go store me the value that the user will enter and show me there. That's why when we entered something nothing happened So for that we go back to our program. And we will store the value that the user will enter in a variable, so that we can reuse it and display it afterwards. We will create a string variable because as I told you just before, the Console.ReadLine() function only supports strings. Then we give it a name for example username Then we put = and there we will assign him the value that the user will enter so we will put the Console function directly here.ReadLine() and don't forget them; And finally, I will display the value that the user entered. So I make a Console.WriteLine and in parentheses, quotation marks I put a small sentence for example You are: then outside the quotation marks I put a + and I put the user_first name variable; I launch the program And so we displayed "What is your first name? "Thene I will give a first name for example Adam. And then you see it shows us "You are: Adam" The program has recovered the value that the user entered. And then he concatenated it with the string "You are:" and displayed it. Now let's go back to our program, and I'll delete all that. I will ask the user to enter "in what year are we? "So I'm going to make a Console.WriteLine(), I type cw tab then in parentheses, I put "" and I write the following question: "In what year are we? » I create a variable that will allow you to store the year that the user will enter I do string I give it as the name annee then = and the Console function.ReadLine() Before launching the program, I would like to modify this value and add one more year to it to then display in the output window next year we will be in 2022 if the user types 2021 So here A Console.writeline and in parentheses, I put " "Next year, we will be in" After the quotes I put a + And there I put the variable year to which I added 1. Now I'm launching the program. He asks me the question in what year are we? I type 2021 I enter and you see we have a little weird result. Basically here, 2021 is considered a string And we can't add something to a string so here he transformed the 1 into a string and concatenate it with 2021. That's why we have this result in the exit window. So how could we do it, knowing that Console.ReadLine() only takes strings as input. Well, we will have to convert the variable that will store the user's input. We will retrieve the value of a String type to convert it to an int type. And that will be the subject of the next video. We will see how to convert a number that the user to enter as a string into an int. I hope it's clear to you and that you liked this video. I'll see you next time in a new video