Hi people! :)
Today we're going to learn how to make our program accept numbers as input. In doing so, we'll learn a little about variables, data types, data type conversions, and arithmetic operations (plus, minus, etc).
Start off by creating a new SharpDevelop project as in yesterday's article, C# Basics: Input and Output. Instead of asking a user for his name, let's write a small program that asks for his age. Use this code:
Console.WriteLine("Enter your age:");
String age = Console.ReadLine();
Console.WriteLine("Only {0}?", age);
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
Nothing new there. This time we are obtaining the user's age and storing it in the age variable, which we then display to the user. It looks like this when you run it (F5):
The String that appears in the second line means that the age variable contains text. Console.ReadLine() gives us the user's input, so we're saying: hey, we want the user's input to go into this thing called age, which contains text (String). These things like name or age, where we store user input (and possibly other stuff) are called variables. String is the data type of the variable: it describes what kind of data (text, numbers, decimal numbers, etc) the variable holds.
In this particular example, we're interested in getting the user's age, which is usually a number. In such a case, it makes more sense to store it as a number. For this, we use the int (integer) data type. However, Console.ReadLine() can only return a String, so we need a means to change that String into an integer. Fortunately, we can do that using Convert.ToInt32():
Console.WriteLine("Enter your age:");
String age = Console.ReadLine();
int usersAge = Convert.ToInt32(age);
Console.WriteLine("Only {0}?", age);
This does not change the output of the program. However, storing the age in an int allows us to do certain arithmetic operations. For example:
Console.WriteLine("Enter your age:");
String age = Console.ReadLine();
int usersAge = Convert.ToInt32(age);
int pensionAge = 65;
int yearsLeftTillPension = pensionAge - usersAge;
Console.WriteLine("{0} years left till pension!", yearsLeftTillPension);
In this case we're using another int variable to store the retirement age, 65. Since usersAge and pensionAge are both integers, we can subtract them and store the result in yearsLeftTillPension. Running the program (F5) brings a grim reminder that pension isn't coming anytime soon:
There's one thing we still have to take care of, though. What happens if a user writes normal text instead of a number? Basically, Convert.ToInt32() explodes and crashes the program (to get out of that, either close the console window, or press Shift+F5 in SharpDevelop):
That's not a nice thing for a program to do, and Father Christmas will certainly take note when preparing his gifts for the next Christmas season. There are many ways in which we can handle this unexpected behaviour, but we aren't ready for this yet. So keep watch for another article here, when all shall be revealed. Until then... happy coding! :)
You're a great writer. I'm learning a lot.
ReplyDeleteGreat sense of humor
Thanks, I appreciate that! :)
Delete