Lab: Supplying Inputs to Programs

Last modified: Jul 22, 2016

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

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

When you are testing and debugging a program, you often want to run the same input through it, over and over, as you work to fix the bugs. This can be rather tiresome if you have to actually type it out, over and over each time. It’s also easy to make mistakes so that you don’t quite run the tests the way you intended.

3 Command Line Parameters

The main function receives two parameters. These are traditionally called argc and argv, although those aren’t the most descriptive of names. These are used to supply command-line options to the program.

  1. Let’s add a few lines to the program:

    #include <iostream> #include <string> using namespace std; int main (int argc, char** argv) { cout << "This program received " << argc << " command parameters." << endl; for (int i = 0; i < argc; ++i) { cout << "argv[" << i << "] is " << argv[i] << endl; } cout << "Now reading from standard in" << endl; string line; getline (cin, line); while (cin) { cout << "I saw: " << line << endl; getline (cin, line); } return 0; }

    Compile and run the program. Note the new output.

    argv is an array of C-strings (character arrays). Each item in this array is one item from the command-line.

    arg tells you how many things are in this array.

  2. Go to your cmd window and run the program like this:

    io_lab a b cdef

    Notice that your command line has 4 items (including the name of the program itself), and the output shows that the argv array has the same 4 items (the program name might be altered slightly, but the others are given exactly).

    Stop the program and run it again like this:

    io_lab "a b" cdef

    Notice that the quotes can be used to group together a string that includes blanks, turning it into a single entry in the command parameter array.

  3. A lot of Windows programs accept command line parameters, though you may not realize this if you always launch them by double clicking on shortcuts or selecting them from the Start menu.

    Go to any empty spot on your desktop, right-click and select New->Shortcut. For the “location of the item”, enter:


    Click Next, give the shortcut any convenient name, and click Finish. Now double-click on your new shortcut to run it. “explorer” is the program that Windows uses to show you your folders. By default, it shows the folder “My Documents”. But we can change that with a command-line parameter. Right-click on your shortcut, select properties, and to the “Target” line add a space and then the characters “c:”. Save this and then run your shortcut again. Notice that the command parameter (“c:”) controls which folder is displayed.

    The lesson here is that even Windows programs that will probably never be invoked directly from a command line will, nonetheless, often benefit from the ability to handle command-line parameters.

    Now let’s return to the shortcut you created earlier. Right-click on the shortcut and select “properties”. The “target” entry holds the same thing you might have typed in the cmd window. Try appending some command line parameters onto the end, click OK to save the changes, then launch the program by double-clicking the shortcut. The output should reflect your new command parameters.

As you progress through the semester, you will be working on programs that take their inputs from a variety of sources. Try to remember that you have many options on how you supply your test inputs, whether you work entirely inside Code::Blocks or work directly at the command line.