home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / tybc4 / for3.cpp < prev    next >
C/C++ Source or Header  |  1993-03-22  |  688b  |  32 lines

  1. // Program that demonstrates using the
  2. // for loop to emulate an infinite loop.
  3.  
  4. #include <iostream.h>
  5. #include <ctype.h>
  6.  
  7. main()
  8. {
  9.    char ch;
  10.    double x, y;
  11.  
  12.    // for loop with empty parts
  13.    for (;;) {
  14.       cout << "\nEnter a number : ";
  15.       cin >> x;
  16.       // process number if non-zero
  17.       if (x != 0) {
  18.         y = 1/ x;
  19.         cout << "1/(" << x << ") = " << y << "\n";
  20.         cout << "More calculations? (Y/N) ";
  21.         cin >> ch;
  22.         ch = toupper(ch);
  23.         if (ch != 'Y')
  24.            break;
  25.       }
  26.       else                         
  27.         // display error message
  28.         cout << "Error: cannot accept 0\n";
  29.    }
  30.    return 0;
  31. }
  32.