Console Programming


Introduction to Console Programming

A console application is a program which runs in an command prompt window. An example of a console application is below:

Console programs do not have the flash, nor the event-driven capabilities of a Windows application, however, they still have their place. For example, where screen space is limited such as a bank ATM, or a device with a very simple display type.


Input and Output Statements

Both input and output statements in console applications reside in the same class, the System.Console1 class. The class method most frequently used to perform console output is the WriteLine method. The generic format for the WriteLine method is:
	Console.WriteLine ("string to write to console")
As we have seen in our error example, to write the string "Hello World!" to the console, we use the statement :
	Console.WriteLine ("Hello World!")
It should be noted that the WriteLine method also writes a carriage return/line feed (CRLF) following the string argument. The similar Console.Write method behaves as does the WriteLine method except without the CRLF.

The class method most frequently used to perform console input is the ReadLine method. The ReadLine method reads the next line of characters from the input stream and assigns these to the variable via the assignment operator. The Console.ReadLine method returns a String data type. When using the ReadLine method, the program will pause and wait until the Enter key is pressed before continuning (i.e. waits for data to be input). The generic format for the ReadLine method is:

	string_variable = Console.ReadLine ()
Data input can be combined with type conversion (as previously discussed) to form a single line statement (although there are still 2 parts to the single line statement). An example looks as follows:
	decInput = Convert.ToDecimal (Console.ReadLine())
The Console.ReadLine statement performs the data input, then passes the input String data directly to the Convert method, which performs the conversion and assigns the converted data to the Decimal variable. Notice there is no intermediate String variable for the ReadLine, the data flows directly from the ReadLine into the Convert method.

1 The Console class belongs to the System namespace. Since the System namespace is the top most hierarchical namespace, this namespace is imported by default so the System can be omitted.


Problem Solving Revisited

Recall our previous discussion of problem solving techniques (forgot?). Now that we understand how to declare and work with variables and perform console input and output, we can tie this information together and start to solve problems. Let's consider the following general steps in implementing a problem solution in VB:
  1. understand the problem

  2. understand what (things) will be required to solve the problem, for example:

  3. plan the solution, using pseudocode (and pencil and paper). Determine what variables may be required, and identify their names and types on paper. The more thoughtful and the better detailed this step is, the more trouble you will save yourself later. Note the following should be present in most, if not all programs:

    1. prompt user for each input (e.g. in a console program, we use the Console.WriteLine or Console.Write method)
    2. get user input (e.g. using the Console.ReadLine method)
    3. convert as necessary (using the Convert class methods)
    4. manipulate input(s) to valid output(s)
    5. display result(s), formatting as necessary

  4. once you begin coding the solution, you will need to declare any constants and/or variables prior to their use

  5. Test and validate actual results match expected results. If testing does not produce correct or expected results, go back to previous step(s).
Keep in mind the steps listed above apply to all programs, whether they have a single input, or dozens; whether they consist of 10 lines of code or 10,000 lines. The process is always the same!

Sample Console Program

Following you will find a relatively simple console program that combines many of the topics discussed in the previous sections. The task that this program will solve is to take a single decimal number as an input value, doubling its value, and displaying the doubled number as output.

The steps in the example are numbered with comments and should be easily understood based upon previous section explanations. Note that step 2 (input and conversion) appears twice; once using two steps (commented out), and one in one combined step. It should be obvious that step two should only be performed once, using one technique or the other. Also of note, the WriteLine method in step 4 using a String operator as well as a String method, both of which are easily explained here. Make sure you can relate the steps below to those listed in the list above (especially #3)!



 Option Explicit On
 Option Strict On

 'program name: NumberDoubler

 'program to read a decimal number from the console and double it

 Module NumberDoubler

      Sub Main()

	'0. declare 2 variables, one for input, one for result
        Dim decNumToDouble As Decimal
	Dim decResult As Decimal

        '1. prompt for input 
        Console.Write("Enter number to double: ")

        '2. read input and convert to decimal - two steps
        'strInput = Console.ReadLine()
        'decNumToDouble = Convert.ToDecimal(strInput)

        '2. read input and convert to decimal -  steps combined
        decNumToDouble = Convert.ToDecimal(Console.ReadLine())

        '3. double the input value
        decResult = decNumToDouble * 2

        '4. output doubled value
        Console.WriteLine("Number doubled is: " & decResult.ToString)

        'dummy readline to keep console window alive
        Console.ReadLine()
       
    End Sub

 End Module



Next Section: Windows Programming Table of Contents


©2007, Mark A. Thomas. All Rights Reserved.