Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- int main()
- {
- int height, h, w;
- do
- {
- cout << "Enter hourglass height: ";
- cin >> height;
- if((height % 2 == 0 || !(height >= 0 && height <= 29)) && height != -1)
- {
- cout << "Invalid Input!" << endl;
- }
- else if(height == 1)
- {
- cout << "#" << endl;
- }
- else if(height != -1)
- {
- //first hourglass border
- for(w = 0; w < height; w++)
- {
- cout << "#";
- }
- cout << endl;
- //loop from first to the half of the height of the hourglass
- for(h = 0; h < height / 2 - 1; ++h)
- {
- //if first loop, then print, or if not, it is spaces
- for(w = 0; w < h+1; w++)
- {
- if(w == 0)
- {
- cout << "#";
- }
- else
- {
- cout << " ";
- }
- }
- //the asterisks between the spaces
- for(w = 0; w < height - (h+1) * 2; ++w)
- {
- cout << "*";
- }
- //the last spaces to print, and the border(#)
- for(w = 0; w < h+1; w++)
- {
- //if last loop, then print, or if not, it is spaces
- if(w == h)
- {
- cout << "#";
- }
- else
- {
- cout << " ";
- }
- }
- cout << endl;
- }
- //loop from half to the last of the height of the hourglass
- for(h = 0; h < height / 2; ++h)
- {
- //if first loop, then print, or if not, it is spaces
- for(w = 0; w < height / 2 - h; w++)
- {
- if(w == 0)
- {
- cout << "#";
- }
- else
- {
- cout << " ";
- }
- }
- //the asterisks between the spaces
- for(w = 0; w < h * 2 + 1; ++w)
- {
- cout << "*";
- }
- //the last spaces to print, and the border(#)
- for(w = 0; w < height / 2 - h; w++)
- {
- //if last loop, then print, or if not, it is spaces
- if(w == height / 2 - h - 1)
- {
- cout << "#";
- }
- else
- {
- cout << " ";
- }
- }
- cout << endl;
- }
- //last hourglass border
- for(w = 0; w < height; w++)
- {
- cout << "#";
- }
- cout << endl;
- }
- }
- while(height != -1);
- //Will go here if height is -1 and will break the loop
- cout << "Program Exiting. Thank you!\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement