Lab: Supplying Inputs to Programs


May 25, 2013

Course Home   e-mail

In this lab, we will look at some of the different ways that basic I/O information can be supplied to a running program.

Probably most of the programs you have written and tested so far have been text-interactive. They were designed to print out a prompt on the screen (cout) requesting a single piece of data. Then they would read that info from the keyboard (cin). Then they would prompt for another piece of input, read another, and so on.


cout << " Enter a number between 1 and 10: "
<< flush ;
int k ;
cin >> k ;
while ( k < 1 || k > 10)
{
cout << " Try again : "
<< flush ;
cin >> k ;
}
cout << " Thanks ! " << endl ;

Enter a number between 1 and 10: 12
Try again : 9
Thanks !

It’s rare for a useful program to actually work that way. If we really want interaction with the human operator, we would expect some sort of Graphic User Interface (GUI) with windows, pop-up dialog boxes, etc. We’re not ready to start writing in that style yet. On the other hand, a lot of very useful programs are not designed to be interactive at all. They take their inputs from files, from other programs, and from parameters specified when the program is launched, and they send their output to files or other programs.

If you are going to write these kinds of programs, you need to know how to control their inputs and outputs so that you can execute and test them.

1 Running the Program
2 Supplying Standard Input
3 Command Line Parameters

[Previous]