CS 150 Introduction to C++ Programming - Spring 1998 

[ Home | Lecture Notes| WebTutor | WebTutor Site Map]


Standard Input of Integers (Exercise 7 Chapter 4)


Standard input is extracted cin, the standard input stream using the extraction operator (>>).
When inputting a number, the extraction operator will "search" for the number by skipping over any leading whitespace (blanks and end of lines).
Input stops when an inappropriate character for that number type is found.
Thus >> reads only as many characters from cin as are needed to form a number.
Any characters after the number (including the character that stopped the input) are still available for the next input operation.

If you want to skip over some of the input, you can use the ignore operator. Ignore takes two arguments,

ignore will either ignore all input characters up to the maximum number or until a stop character is found, whichever comes first.

(see pages 141-142 in the textbook).

Consider the following input data:

14 21 64
19 67 91
73 89 27
23 96 47

What are the values of the following variables of type int after the following program segment is executed?

	cin >> int1;
	cin.ignore(200,'\n');
	cin >> int1 >> int3;
	cin.ignore(200, '\n');
	cin >> int4;
  1. int1

  2. int2

  3. int3

  4. int4

 

Choose one when done
   

Copyright chris wild 1998.
For problems or questions regarding this web contact [Dr. Wild (e-mail:wild@cs.odu.edu].
Last updated: February 10, 1998.