BHXSpecter

Exercise 1 Done (Again!)

Nov 7th, 2011
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. const int SENTINEL = -999;
  4.  
  5. int main()
  6. {
  7.     double n = 0, sum = 0, temp = 1;
  8.    
  9.     cout << "Enter a number(Enter -999 to exit): ";
  10.     cin >> n;
  11.     cout << endl;
  12.  
  13.     while (n != SENTINEL)
  14.     {
  15.         if(n < 0)
  16.         {
  17.             cout << "Only positive numbers!" << endl;
  18.         }
  19.         else if(!cin.good())
  20.         {
  21.             cout << "No letters!" << endl;
  22.             cin.clear();
  23.             cin.ignore(100, '\n');
  24.         }
  25.         else if (n == 0)
  26.         {
  27.             sum = 1;
  28.             cout << sum << endl;
  29.         }
  30.         else
  31.         {
  32.             while (n != 0)
  33.             {
  34.                 temp = temp * n;
  35.                 n = n - 1;
  36.             }
  37.             cout << temp << endl;
  38.         }
  39.        
  40.         temp = 1;
  41.         cout << "Enter a number(Enter -999 to exit): ";
  42.         cin >> n;
  43.         cout << endl;
  44.     }
  45.  
  46.     return 0;
  47. }
  48.  
  49.  
Advertisement
Add Comment
Please, Sign In to add comment