Making a hexagon creator

Hey guys, im writing a program that takes a user's input and prints out a hexagon based on it.
i've nearly finished it but im a little stuck on two things the first is getting it to print horizontal lines as its supposed to do one row of horizontal line then one row of spaces.

the second is error checking i know in c# there is try parse to check if an input matches the data type but i don't know what the equivalent is in c++.

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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include <iostream>
#include <string>

using namespace std;
//This class is to store all the functions needed to create the hexagons
class Hexagon
{
private:
	//size stores the sizeInput value from the main class
	//type stores the typeInput value from the main class
	//fill stores the content to fill the hexagon
	int size;
	int type;
	string fill;
public:
	//int getSize() {return size;}
	//int getType() {return type;}
	// basic constructor and deconstructor to allow instances to be created
	Hexagon(int sizeInput = 0, int typeInput = 0) :	size(sizeInput), type(typeInput)
	{
	}
	
	~Hexagon()
	{
	}
	//function to create empty and filled hexagons
	void BlankAndFill(int size, int type)
	{
		//checks which type of hexagon user wants and sets fill accordingly
		if(type == 1)
		{
			fill = " ";
		}
		else
			if(type == 2)
			{
				fill = "+";
			}
			//prints out what type of hexagon is being made
			cout << "Type " << type << endl;
		//stores row number for use as a counter
		int rows = 0;
		//prints out first half of the hexagon
		for(; rows < size+1;rows++)
		{	
			//for first row prints out top of hexagon
			if(rows==0)
			{
				//this for loop prints out required spaces
				for(int spaces = 1+size; spaces > rows;spaces--)
			{
				cout << " ";
			}
				//this for loop prints out top of hexagon
				for(int tops = 0; tops < size;tops++)
				{
					cout << "_";
				}
				cout << endl;
			}
		else
		{
				for(int spaces = 1+size;spaces > rows;spaces--)
				{
					cout << " ";
				}
				cout << "/";
				//this for loop prints out the content of the hexagon
				for(int content=0; content < size + ((rows - 1) * 2);content++)
				{
					cout << fill;
					
				}
				cout << "\\";
				cout << endl;
		}
		}
		//this for loop prints out the second half of the hexagon
		for(; rows >= 2; rows--)
		{		
			if(rows == 2)
			{
				for(int spaces = 2+size;spaces > rows;spaces--)
				{
					cout << " ";
				}
				cout << "\\";
				//this for loop prints out the bottom line of the hexagon
				for(int bottoms = 0;bottoms < size;bottoms++)
				{
					cout << "_";
				}
				cout << "/";
				cout << endl;
			}
			else
			{
				for(int spaces = 2+size;spaces > rows;spaces--)
				{
					cout << " ";
				}
				cout << "\\";
				for(int contents=0; contents < size + ((rows - 2) *2);contents++)
				{
					cout << fill;
				}
				cout << "/";
				cout << endl;
			}
		}
		//empties the rows variable to allow multiple hexagons to be printed
		rows =0;
	}



	//function to create hexagons filled with horizontal or vertical lines
		void XandY(int sizeInput, int type)
	{
		//spacer stores spaces to be used to properly space hexagons and add gaps inside them
		//counter stores number of iterations that have currently passed so inside spaces get placed properly
		string spacer = " ";
		int counter =0;
		if(type == 3)
		{
			fill = "-";
		}
		else
			if(type == 4)
			{
				fill = "|";
			}
			cout << "Type " << type << endl;
		int rows = 0;
		for(; rows < size+1;rows++)
		{
			if(rows==0)
			{
				for(int spaces = 1+size; spaces > rows;spaces--)
			{
				cout << spacer;
			}
				for(int tops = 0; tops < size;tops++)
				{
					cout << "_";
				}
				cout << endl;
			}
		else
		{
				for(int spaces = 1+size;spaces > rows;spaces--)
				{
					cout << spacer;
				}
				cout << "/";
				for(int stars=0; stars < size + ((rows - 1) * 2);stars++)
				{
					//checks if current iteration is even and if type is 4 
					//if so prints a spacer instead of the filler
					if(counter % 2 == 0 && type == 4)
					{
						cout << spacer;
						counter++;
					}
					else
					{
						cout << fill;
						counter++;
					}
				}
				cout << "\\";
				cout << endl;
		}
		}
		//empties counter value so iteration count is reset
		counter = 0;
		for(; rows >= 2; rows--)
		{		
			if(rows == 2)
			{
				for(int spaces = 2+size;spaces > rows;spaces--)
				{
					cout << spacer;
				}
				cout << "\\";
				for(int bottoms = 0;bottoms < size;bottoms++)
				{
					cout << "_";
				}
				cout << "/";
				cout << endl;
			}
			else
			{
				for(int spaces = 2+size;spaces > rows;spaces--)
				{
					cout << spacer;
				}
				cout << "\\";
				for(int stars=0; stars < size + ((rows - 2) *2);stars++)
				{
					if(counter % 2 == 0 && type == 4)
					{
						cout << spacer;
						counter++;
					}
					else
					{
						cout << fill;
						counter++;
					}
				}
				cout << "/";
				cout << endl;
			}
		}
		rows =0;
		counter = 0;
	}
	//takes sizeInput and typeInput and sets them to local variables
	void setSize(int size) {size = size; return;}
	void setType(int typeInput) {type = typeInput; return;}
};
int main()
{
	//sizeInput and typeInput stores size/type of hexagon from user
	//option stores whether use wants to continue making hexagons
	int sizeInput, typeInput,optionInput = 1;

	//this while loop keeps the program running until user chooses to exit
	while(optionInput == 1)
	{
		//asks user for input then stores it in sizeInput variable
		cout << "Please enter size of hexagon \n";
		cin >> sizeInput;
		cout << endl;
		//checks if size inputted is less then 1, if it is then asks for another number
		if(sizeInput < 1)
		{
			cout << "Please enter only positive integers \n";
			cout << endl;
			cout << "Please enter size of hexagon \n";
			cin >> sizeInput;
			cout << endl;
		}
		//allows user to choose type of hexagon they want and stores it in typeInput variable
		cout << "Please choose type of hexagon \n";
		cout << "1: Type 1 Empty\n";
		cout << "2: Type 2 Filled\n";
		cout << "3: Type 3 Horizontal\n";
		cout << "4: Type 4 Vertical\n";
		cin >> typeInput;
		//creates an instance of hexagon and passes size and type into hexagon class
		Hexagon h1(sizeInput, typeInput);
		//checks typeInput variable the user inputted and runs the appropriate function
		if(typeInput == 1 )
		{
			h1.BlankAndFill(sizeInput, typeInput);
		}
		if(typeInput == 2)
		{
			h1.BlankAndFill(sizeInput, typeInput);
		}
		if(typeInput == 3)
		{
			h1.XandY(sizeInput, typeInput);
		}
		if(typeInput == 4)
		{
			h1.XandY(sizeInput, typeInput);
		}
		//checks if user wants to draw another hexagon and stores choice in optionInput variable
		cout << "Would you like to draw a hexagon? \n";
		cout << "1: Yes \n";
		cout << "2: No \n";
		cin >> optionInput;
}
}

help would be greatly appreciated
Last edited on
needed to ask different questions about same program and thought it'd be better to use my old topic instead of making a new one.

thought i'd also explain my attempt was to add a second counter in the rows loop then an if statement that prints out blank spaces if the row is even and lines if not.
but it instead just prints /----\ over and over again.
Last edited on
Topic archived. No new replies allowed.