Advertisement
aid8

Hourglasss

Feb 28th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.43 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     int height, h, w;
  7.  
  8.     do
  9.     {
  10.         cout << "Enter hourglass height: ";
  11.         cin >> height;
  12.        
  13.         if((height % 2 == 0 || !(height >= 0 && height <= 29)) && height != -1)
  14.         {
  15.             cout << "Invalid Input!" << endl;
  16.         }
  17.        
  18.         else if(height == 1)
  19.         {
  20.             cout << "#" << endl;   
  21.         }
  22.        
  23.         else if(height != -1)
  24.         {
  25.             //first hourglass border
  26.             for(w = 0; w < height; w++)
  27.             {
  28.                 cout << "#";
  29.             }
  30.             cout << endl;
  31.            
  32.             //loop from first to the half of the height of the hourglass
  33.             for(h = 0; h < height / 2 - 1; ++h)
  34.             {
  35.                 //if first loop, then print, or if not, it is spaces
  36.                 for(w = 0; w < h+1; w++)
  37.                 {
  38.                     if(w == 0)
  39.                     {
  40.                         cout << "#";
  41.                     }
  42.                     else
  43.                     {
  44.                         cout << " ";
  45.                     }
  46.                 }
  47.                
  48.                 //the asterisks between the spaces
  49.                 for(w = 0; w < height - (h+1) * 2; ++w)
  50.                 {
  51.                     cout << "*";
  52.                 }
  53.                
  54.                 //the last spaces to print, and the border(#)
  55.                 for(w = 0; w < h+1; w++)
  56.                 {
  57.                     //if last loop, then print, or if not, it is spaces
  58.                     if(w == h)
  59.                     {
  60.                         cout << "#";
  61.                     }
  62.                     else
  63.                     {
  64.                         cout << " ";
  65.                     }
  66.                 }
  67.                 cout << endl;  
  68.             }
  69.            
  70.             //loop from half to the last of the height of the hourglass
  71.             for(h = 0; h < height / 2; ++h)
  72.             {
  73.                 //if first loop, then print, or if not, it is spaces
  74.                 for(w = 0; w < height / 2 - h; w++)
  75.                 {
  76.                     if(w == 0)
  77.                     {
  78.                         cout << "#";
  79.                     }
  80.                     else
  81.                     {
  82.                         cout << " ";
  83.                     }
  84.                 }
  85.                
  86.                 //the asterisks between the spaces
  87.                 for(w = 0; w < h * 2 + 1; ++w)
  88.                 {
  89.                     cout << "*";
  90.                 }
  91.                
  92.                 //the last spaces to print, and the border(#)
  93.                 for(w = 0; w < height / 2 - h; w++)
  94.                 {
  95.                     //if last loop, then print, or if not, it is spaces
  96.                     if(w == height / 2 - h - 1)
  97.                     {
  98.                         cout << "#";
  99.                     }
  100.                     else
  101.                     {
  102.                         cout << " ";
  103.                     }
  104.                 }
  105.                 cout << endl;
  106.             }
  107.            
  108.             //last hourglass border
  109.             for(w = 0; w < height; w++)
  110.             {
  111.                 cout << "#";
  112.             }
  113.             cout << endl;
  114.         }
  115.     }
  116.     while(height != -1);
  117.    
  118.     //Will go here if height is -1 and will break the loop
  119.     cout << "Program Exiting. Thank you!\n";
  120.     return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement