Professor Mustard Programming
 

Programming 101 - Unit 01 - The Basics

01c) Console Input
 
After going on for so long about variables, you're going to find this lesson quick and easy. You see, ReadLine() is actually just another way of assigning a value to a variable. Let's look at the following code:

Console.Write("Enter your name and hit ENTER: ");
string name = Console.ReadLine();
Console.WriteLine("Hello, " + name + "!")



Poof! Mission complete. In just three lines, you've prompted for input, stored input, and used input. Putting a Write() statement just before the ReadLine() isn't even manditory, it's just that you typically need some way of telling the user what kind of input you're expecting. It's only polite.

Perhaps the only wrinkle is that ReadLine() always returns strings. In order to get numbers, you'll have to do that convert thing again:

Console.Write("Enter your age and hit ENTER: ");
int age = Convert.ToInt32( Console.ReadLine() );
Console.Write("Enter your height and hit ENTER (you can use decimals): ");
double height = Convert.ToDouble( Console.ReadLine() );
Console.WriteLine("So, you are {0} years old, with a height of {1}.", age, height);



Remember, without proper error handling (which we'll cover soon enough), the program will crash if it tries to convert a string like "penguin" into a number!

What about that ReadLine() we've been putting at the end of each program so far? Well, we've really just been taking advantage of C#'s input system. We've been using ReadLine() at the end of the program so that C# basically sits there, patiently waiting for the user to finish their input and hit ENTER. When ENTER is pressed, the program gathers all the input that the user's entered and does... nothing with it, because we're not assigning the value of ReadLine() to anything! So C# just shrugs its digital shoulders, forgets the data the user entered, and moves on. Still, now that we know what ReadLine() actually does, we could at least prompt the user to hit ENTER at the end of the program, right?

User input, in any form, is always one of the trickiest aspects of programming. It's actually difficult to believe the number of unexpected ways that a user can crash or screw up your program! We'll learn better ways to cope with user stupidity in future lessons, but for now, just remember: any portion of code that depends on external communication, whether it would be console input, a text file, or a network drive, is a potential point of failure, and should be treated with all due caution. With that, however, let's move on with your code sample:



using System;

namespace C_ConsoleInput
{
  class Class1
  {
    [STAThread]
    static void Main(string[] args)
    {
      // Prompt the user for their name
      Console.Write("Enter your name and hit ENTER: ");

      // Read in the input; assign to "name"
      string name = Console.ReadLine();

      // Now we can use "name" just like any other variable
      Console.WriteLine("Hello, " + name + "!");

      // Let's do it again, only this time ask for a pizza type
      Console.Write("Enter the pizza you want to order and hit ENTER: ");
      string pizza = Console.ReadLine();

      // We'll use both variables we've collected for this one
      Console.WriteLine("Hello, {0}! One {1} pizza coming right up!", name, pizza);

      // Using ReadLine() with numeric datatypes
      Console.Write("Enter your age and hit ENTER: ");
      int age = Convert.ToInt32( Console.ReadLine() );
      Console.Write("Enter your height and hit ENTER (you can use decimals): ");
      double height = Convert.ToDouble( Console.ReadLine() );
      Console.WriteLine("So, you are {0} years old, with a height of {1}.", age, height);

      // Let's ask the user to hit "enter", instead of leaving them a blank prompt
      Console.Write("Program complete! Hit enter to exit...");
      Console.ReadLine();
    }
  }
}