home *** CD-ROM | disk | FTP | other *** search
/ Beginning C++ Through Gam…rogramming (2nd Edition) / BCGP2E.ISO / source / chapter05 / give_me_a_number.cpp next >
Encoding:
C/C++ Source or Header  |  2004-04-11  |  629 b   |  33 lines

  1. // Give Me a Number
  2. // Demonstrates default function arguments
  3.  
  4. #include <iostream>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. int askNumber(int high, int low = 1);
  10.  
  11. int main()
  12. {
  13.     int number = askNumber(5);
  14.     cout << "Thanks for entering: " << number << "\n\n";
  15.     
  16.     number = askNumber(10, 5);
  17.     cout << "Thanks for entering: " << number << "\n\n";
  18.  
  19.     return 0;
  20. }
  21.  
  22. int askNumber(int high, int low)
  23. {
  24.     int num;
  25.     do
  26.     {
  27.         cout << "Please enter a number" << " (" << low << " - " << high << "): ";
  28.         cin >> num;
  29.     } while (num > high || num < low);
  30.  
  31.     return num;
  32. }
  33.