hourglass made out of "*" with an odd number inputs

I'm trying to write a code for an an asterisk hourglass, the constraints are that the user enters the number of asterisks for the top row and how many rows from the top row to the middle. Also no row is allowed to have less than two ’*’s.

heres what a top row= 7, half height = 3 should look like ("_"=blank spaces)

*******
_*****_
__***__
_*****_
*******

My problem is that my code only works if the number of rows from the top to the middle is half of the top row. What am i doing that is limiting my odd number potential? And is it just me or is it impossible to use only a while loop and three for loops?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
  /*
	File: hourglass.cpp
	Created by: 
	Creation Date: 10/8/2014
	Synopsis: prints an hour glass using 
	  asterisks and an maximum top row from user
*/

#include <iostream>
#include <cmath>
using namespace std;

int main()
{

  // Declare and initialize variables
	
	int row_top(0);
	int row(0);
	int i(0);
	int k(0);
	int j(0);


  // Repeatedly prompt for top row size until valid value is entered

cout << "Enter size of the top row: " ;
cin >> row_top;

	while(row_top < 3)
	{
	cout << "Size of the top tow must be at least three." << endl;
	cout << "Enter size of the top row again: "; 
	cin >> row_top;
	}


  // Repeatedly prompt for the number of rows until valid value is entered

cout << "Enter number of rows: ";
cin >> row;

	while(row == 0 || row_top/row < 2.0  || row < 1.0 )
	{
	cout << "Invalid number of rows." << endl;
	cout << "Enter number of top row again: "; 
	cin >> row;
	}


  // Print the hour glass
cout << endl;

	for (i=1; i <= row * 2 ; i++)
	{
		if (i <= row+1)
		{	for (j=1; j <= i-1; j++)
			{ 
			cout << " ";
			}
		
	
	
			for (k=1; k <= row_top-(i*2-2); k++)
			{
			cout << "*";
			}
			if (row != i)
			{
			cout << endl;
			}
		}
		else 
		{
			for (j=row;  j >= i-(row-1); j--)
			{ 
			cout << "o";
			}
		
	
	
			for (k=1; k >= row_top-(i*2-2); k--)
			{
			cout << "*";
			}
			cout << endl;	
		}
	}
		

  // end program
  return 0;
}
Last edited on
Topic archived. No new replies allowed.