Source of pausing.cpp


  1: //pausing.cpp
  2: //Illustrates how to make a program pause and wait for the user to press
  3: //Enter before continuing, an often useful "user interface" feature.

  5: #include <iostream>
  6: using namespace std;

  8: int main()
  9: {
 10:     cout << "\nThis program illustrates how to create a \"pause\" in "
 11:         "the output of a program,\nso that a user may read the preceding "
 12:         "output before continuing. The user then\ncontinues by pressing "
 13:         "the Enter key.\n";

 15:     //The following code causes the program to pause and wait
 16:     //for the user to press the Enter key before continuing.
 17:     //BUT ONLY IF INPUT STREAM cin IS EMPTY WHEN THE CODE EXECUTES
 18:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n'); //1

 20:     //The following three code sections are identical to those in
 21:     //simple_io.cpp, except for the addition of three instances of
 22:     //cin.ignore(80, '\n');
 23:     int someInteger;
 24:     cout << "\nEnter an integer value here: ";
 25:     cin >> someInteger;   cin.ignore(80, '\n'); //2
 26:     //Study carefully the above useful C++ "idiom" for reading
 27:     //an integer and ignoring whatever else in on the same line.
 28:     cout << "The integer you entered was " << someInteger << ".\n";

 30:     double someReal;
 31:     cout << "\nEnter a real number value here:\n";
 32:     cin >> someReal;  cin.ignore(80, '\n'); //3
 33:     cout << "The real number you entered was " << someReal << ".\n";

 35:     char firstChar, secondChar;
 36:     cout << "\nEnter a character value here: \t\t";
 37:     cin >> firstChar;  cin.ignore(80, '\n'); //4
 38:     cout << "Enter another character value here: \t";
 39:     cin >> secondChar; cin.ignore(80, '\n'); //5
 40:     cout << "The first character entered was " << firstChar << ".\n";
 41:     cout << "The second character entered was " << secondChar << ".\n";
 42:     cout << endl;

 44:     //A pause at the end of the program
 45:     cout << "Press Enter to continue ... ";  cin.ignore(80, '\n'); //6
 46: }