String User Input

Creating user input for your application can make your program 100X better than if it didn’t, this is because it provides your user with the ability to give their opinion or choice. This is section is extremely important in your further learning in the C# language, so pay close attention. A program where your user can’t give their ideas is not very useful at its core. To display the user input function, we’ll create a new console program. We’ll call it, “usrInputEx”. First we’ll look at the finalized code and find where it accepts input.

Open your program, I want you to type the three WriteLine lines that were in the last example.

Next, I would like you to list your variables in the correct positions. I’d like you to label the strings as follows: For the name “strName”, for the gender “strGen”, and for the birthday “strDate”.

After that, we’ll add the input commands where the program with compute the input that the user types in and then uses the rest of the program in the last step.

You’ve got all the necessary information to complete the concatenate line. I want you to type the following line. ‘Console.WriteLine(String.Concat(strName, " is ",strGen," and was born on ",strDate.ToString()));’. Note that the params have spaces on either side of the text; this is important because if they weren’t there, the whole sentence would be squished together. Also, each individual word or sentence that is separated by variables has a comma in between; this is also very important.

If you start up your program, you’ll come up with a screen that will ask for you name. Once you’re done answering the information it asks for, it’ll come up with a screen showing you all the information into a sentence.

Viola! You’ve created a program where it accepts user input and processes into a sentence. Next, we’ll be dealing with a way to better get user input and process user’s information.