COSC 1315

Programming Fundamentals

Standard Input

Revised:  January 29, 2007
By Richard G. Baldwin

File:  Pf00130.htm
Practice Text


Preface

This lesson was written specifically for the benefit of my students in COSC 1315, Fundamentals of Programming.  The lesson was written under the assumption that those students have no prior programming knowledge when they enroll in the course.

Another browser window

I recommend that you open another copy of this document in a separate browser window so that you can view the code and the discussion of that code at the same time.

Sample Program

The C++ program in Listing 1 illustrates simple standard input from the keyboard.  Note that this program uses object-oriented syntax as explained in Lesson 110.

/*File:  Input01.cpp
This C++ program illustrates simple standard
input from the keyboard. 

When the type of input data matches the types 
of the variables into which the data is stored,
the program runs correctly producing the 
following screen output:

Enter three values separated by spaces
1 2 3
1 2 3
Press any key to continue

However, when the type of the input data fails 
to match the types of the variables into which
the data is stored, the program fails, as shown
in the following screen output, but doesn't give
any other indication that a failure has occurred.

Enter three values separated by spaces
1 1.5 2
1 1 -858993460
Press any key to continue

************************************************/

#include <iostream>
using namespace std;

class Input01{ 
  public:
  static void classMain(){
    //Instantiate an object in dynamic memory
    // and save its reference in a pointer 
    // variable.
    Input01* ptrToObject = new Input01();

    //Invoke an instance function on the object.
    ptrToObject -> doSomething();
  }//End classMain function
  //-------------------------------------------//

  //An instance function of the Input01 class
  void doSomething(){
    int first;
    int second;
    int third;

    //Display a prompt
    cout << "Enter three values separated by";
    cout << " spaces\n";

    //Get three input values.
    cin >> first >> second >> third;
    //Alternative syntax
    //cin >> first;
    //cin >> second;
    //cin >> third;

    //Display the three input values
    cout << first << " " << second << " ";
    cout << third << endl;
  }//end doSomething function
};//End Input01 class
//---------------------------------------------//

int main(){
  Input01::classMain();
  return 0;
}//end main

Listing 1

When input data type matches

The method named doSomething creates three variables named first, second, and third that are designed to store data values of type int.

(For now, suffice it to say that a variable is a named pigeonhole in memory where a data value can be stored.  Also for now, suffice it to say that that the type int is a type that is designed to describe whole numbers, or integers ranging from zero up to a large maximum magnitude.  Thus, the three variables are created to store whole numbers ranging from zero to a large maximum magnitude.)

When the type of input data matches the types of the variables into which the data is stored, the program runs correctly producing the following screen output:

Enter three values separated by spaces
1 2 3
1 2 3
Press any key to continue

When input data type doesn't match

However, when the type of the input data fails to match the types of the variables into which the data is to be stored, the program fails, as shown in the following screen output.

Enter three values separated by spaces
1 1.5 2
1 1 -858993460
Press any key to continue

(The exact nature of the failure may differ depending on which compiler was used to compile the program.)

A rather obscure failure

Unfortunately, other than the bad data, the program doesn't give any indication that a failure has occurred.

This failure wouldn't have been evident if the captured data hadn't been displayed.

The cin Object vs.. the cout Object

You are already familiar with the use of the cout object for printing or displaying program data on the computer screen (see Lessons 105 and 106).  In computer jargon, we would say that the cout object provides a program output capability.

Keyboard input

The cin object provides the program input counterpart to the cout object.  Although things are really much more complex than this, for the purposes of this course, let's simply say that the cin object can be used to read data from the computer keyboard and to deposit that data into variables that have been created within the program.

For example, the boldface statement in Listing 1 reads three values typed at the keyboard, (separated by space characters), and deposits those values into the variables named first, second, and third in that order.

Alternatives

Listing 1 also shows an alternative syntax.  For example, it would work just as well to separate the input operation into three statements as shown in the comments under Alternative syntax in Listing 1.

It would also work just as well to separate the entry of the three data values at the keyboard by pressing the enter key following each value rather than separating the three data values using the space character.

The extraction operator, >>

In the earlier lesson 105, you learned about the insertion operator (<<) that is used with cout to insert data into the output stream.

The cin object uses an extraction operator (>>) to extract data from the standard input and to deposit that data into named variables.  The use of the extraction operator is shown in boldface in Listing 1.

Just memorize the syntax

Unfortunately, this is another of those occasions where I have to tell you that you really don't have the technical background to understand what is going on with respect to the boldface statement in Listing 1.  Therefore, the best that I can do for you at this point is to simply tell you to memorize the syntax so that you can use the cin object to input data from the keyboard for the programs that you write.

And a word of caution

Also, as illustrated earlier, I can tell you to be very careful to make certain that when you read a value from the keyboard, you deposit that value into a variable whose type matches the type of data entered at the keyboard.  (You will learn about data types in a future lesson.)


Copyright 2005, Richard G. Baldwin.  Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.

About the author

Richard Baldwin is a college professor (at Austin Community College in Austin, TX) and private consultant whose primary focus is a combination of Java, C#, and XML. In addition to the many platform and/or language independent benefits of Java and C# applications, he believes that a combination of Java, C#, and XML will become the primary driving force in the delivery of structured information on the Web.

Richard has participated in numerous consulting projects and he frequently provides onsite training at the high-tech companies located in and around Austin, Texas.  He is the author of Baldwin's Programming Tutorials, which have gained a worldwide following among experienced and aspiring programmers. He has also published articles in JavaPro magazine.

In addition to his programming expertise, Richard has many years of practical experience in Digital Signal Processing (DSP).  His first job after he earned his Bachelor's degree was doing DSP in the Seismic Research Department of Texas Instruments.  (TI is still a world leader in DSP.)  In the following years, he applied his programming and DSP expertise to other interesting areas including sonar and underwater acoustics.

Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.

Baldwin@DickBaldwin.com

-end-