Console I/O

One of the most basic program functions is writing to the user console. However, this is also one of the most poorly documented.

Basically, the user console is a text only interface.

Typically, Windows applications don't use this interface, preferring the graphical GUI instead. However, basic high school C++ classes use only the text-only user console. Thus the problem.

All C console applications start by calling the main() function which is passed the number of command line arguments and an array of argument strings.

int main(int argc, char **argv)
{
    // argc is the number of arguments - >=1
    // argv[0] is the name of the executable
    // argv[1] thru argv[argc] are the arguments
  return 0;  // Required, zero means no error
             //   value can be tested by bat file or calling routine
}


conio.h

This is my preferred way to read the keyboard and write to the screen. Unfortunately, this is DOS only - it will not work in unix. The important commands are Together, these commands allow you to create menus where you can use the arrow keys to move a cursor and the enter key to select an option.

The stream method does NOT allow this flexibility. Instead you need to use ANSI terminal commands which are not supported everywhere. (Under DOS, Ansi.sys has to be loaded in config.sys.)


streams.h

This is the new C++ way to perform I/O. However, it does not allow the control of color or xy placement. (This is called an improvement? :)

Unfortunately, this is the only form of I/O taught in modern AP Computer Science classes.

  #include <iostream>

    cout << someString << "a string constant" << anInt << endl;
The endl constant is a nice touch providing a system independent way of moving to the next line.
    cin >> a;
One problem with cin is that the first time that it is used, it moves the cursor to the next line.


C++ Builder

To generate a console application, select File / New... / New / Console Wizard and click Finish.

Here is a basic program which uses stream I/O - 143 Kb.

#pragma hdrstop
#include <condefs.h>   // For all console applications
#include <iostream.h>  // For cout
#include <conio.h>     // For getch

//---------------------------------------------------------------------------
#pragma argsused
int main(int argc, char **argv)
{
 cout << "Any string";
 getch();                // This pauses the program so you 
 return 0;               //   can read the output
}

This program converts keypresses into hex codes - 56 Kb.
#pragma hdrstop
#include <condefs.h>   // For all console applications
#include <iostream.h>  // For cout
#include <conio.h>     // For getch

//---------------------------------------------------------------------------
#pragma argsused
int main(int argc, char **argv)
{
  int c;
  do {
  c = getch();
  if (c==0)        // Zero means extended character
  {
    c = getch();   // Read the second byte
    printf("DOS extended character   00 %02X\n", c);
  }
  else
  {
    printf("Simple ASCII character      %02X\n", c);
  }
  }  while (c^=0x1b);
  getch();
  return 0;
}


Visual C++

Stream based I/O

When used with cin, the getline function that is supplied in the string library (VC++ 6.0) contains a design problem - it requires the user to hit enter twice. It also places a null string in the input buffer (ie, you have to test for and remove extra null strings). Microsoft Knowledge Base Article - 240015

This can be fixed by editing line 165 of the file: Line 165 should be: instead of: iostream.html contains information on using the iostream template. In particular, it discusses namespace problems and why template based programs are compiler dependent.


Author: Robert Clemenzi - clemenzi@cpcug.org
URL: http:// cpcug.org / user / clemenzi / technical / Languages / Console_IO.htm