ReadLine( ) and Input

In Chapter 3, you learned how to use WriteLine( ) to output messages to the console. Up until now, though, we haven’t shown you how to get any input from your users. The looping statements that we’ll introduce in the next section often go well with user input, so we’ll cover that now. C# has a ReadLine( ) statement that corresponds to WriteLine( ), and as you might guess, it reads in a string from the console into a string variable. The use of ReadLine( ) is rather simple. To take input from the console and assign it to the string inputString, you’d do this:

string inputString;
inputString = Console.ReadLine( );

Whatever the user types at the console, until he presses Enter—in other words, one line—is assigned to inputString.

It usually helps to give the user a prompt to let him know what he should be entering at a ReadLine( ), so it makes sense to start with a WriteLine( ):

Console.WriteLine("Enter your input string.")
inputString = Console.ReadLine( );

Often, though, you’ll want the user to enter his input on the same line, immediately after the prompt. In that case, you can use Write( ) instead of WriteLine( ). Write( ) outputs text to the console, the same as WriteLine( ), but it doesn’t send a newline character. In other words, if you follow a Write( ) with a ReadLine( ), the cursor waits at the end of the output for the user’s input:

Console.Write("Enter your name here: ");
string userName = Console.ReadLine( );

You’ll notice that ReadLine( ) accepts only a string, which can be a problem if you want your user to enter a number that you want to calculate with. C# will happily accept the number, but as a string, and you’ll quickly find that C# doesn’t implicitly convert from string to int, for example. So, this won’t work:

Console.Write("Enter your age: ");
string myInt = Console.ReadLine( );
if (myInt >= 18)
   Console.WriteLine("You may buy a ticket.");

Fortunately, this is an easy problem to fix. You just need to convert the string to an int32 type, and for that, C# offers the Convert class. You can fix the error in the code we just showed you like this:

Console.Write("Enter your age: ");
string inputAge = Console.ReadLine( );
int myInt = Convert.ToInt32(inputAge);
if (myInt >= 18)
   Console.WriteLine("You may buy a ticket.");

You can even combine the input and conversion into a single step, like this:

int myInt = Convert.ToInt32(Console.ReadLine( ));

The Convert class can convert to a number of classes, including ToInt32 (and various other types of int), ToDouble, ToBoolean, ToChar, and ToString. However, there are some limitations, most of which are easy to predict. You can convert a char to a string easily (you get a string with just one character), but you can’t convert a string to a char if it’s more than one character long. You can’t convert a char to a double, no matter what you do, because the two types are incompatible. This means that if your code requires a conversion, you should include some error-checking code to ensure that your program doesn’t crash if the user enters bad data—for example, if the user enters “Q” in the age-checking code we just showed you. We’ll get to that in Chapter 16; for the time being, we’ll assume that you have perfect users who always do just what they’re told, and never enter bad data. (When you’re done with them, can you send them over our way? We’d love to meet them.)

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset