Turn if statements into functions

The code ask the user to choose a shape to draw, how would I turn the if statements into functions. By doing this can I then prompt the user to choose how many shapes they want drawn.

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
#include <iostream>
#include <math.h>
#include <iomanip>
#include <string>
#include <cstdlib>

using namespace std;
void printmessage ()
{
    cout<< "Good job thanks for playing:)";
}
int main()
{
	float shape = 0;
	int side_length, num_asterisk;
	int squareWidth, squareHeight;
	int size1, size2, totalRows, totalCols;
	int circle_radius;
	int number, rows, cols;

	cout << "Hello you will be prompt to draw a shape!" << endl;
	cout << endl;
	cout << "The shape will be drawn with the asterisk *" << endl;
	cout << endl;
	cout << "**************** MENU ******************" << endl;
	cout << "*                                  	*" << endl;
	cout << "*1) BOX (Filled)                   	*" << endl;
	cout << "*2) BOX (Not Filled)               	*" << endl;
	cout << "*3) BOX with an X inside           	*" << endl;
	cout << "*4) CIRCLE                         	*" << endl;
	cout << "*5) X                              	*" << endl;
	cout << "*                                  	*" << endl;
	cout << "**************** MENU ******************" << endl;

	do
	{
    	cout << "Enter a number from the menu above to begin drawing a shape: ";
    	cin >> shape;
    	cout << endl;
	}while(shape < 1 || shape > 5);

if(shape == 1)
{
	cout << "You choose to draw a Box Filled!!!" << endl;
	do
	{
 	cout << "Enter the length for the side of the box: ";
 	cin >> side_length;
 	cout << endl;
	}while (side_length <= 1);

	num_asterisk = side_length * side_length;

	for (int i = 1; i < num_asterisk + 1; i++ )
	{
    	cout << "*";
    	if((i %side_length) == 0)
    	{
        	cout << "\n";
    	}
	}
}
else if(shape == 2)
{
	cout << "You choose to draw a Box not Filled!!!" << endl;
do
{
	cout << "Enter Height of the box:  ";
	cin >> squareHeight;
	cout<<endl;
}while(squareHeight <= 1);

do
{
	cout << "Enter Width of the box:   ";
	cin >>  squareWidth;
	cout<<endl;
}while(squareWidth <= 1);

  for(int width=1; width<=squareHeight; width++)
	{
   if(width <= 1)
   for(int width=1; width<=squareWidth; width++)
        	{
            	cout<< "*";
        	}
    	else if(width<squareHeight)
    	{
        	cout<< endl;
for(int width2=1; width2<=squareWidth; width2++)
       	{
  	if(width2==1 || width2==squareWidth)
                	cout<< "*";
            	else
                	cout<< " ";
       	}
    	}
    	else
    	{
        	cout<< endl;

for(int width3=1; width3<=squareWidth; width3++)
        	{
            	cout<<"*";
        	}
    	}
	}
}

else if(shape == 3)
{
cout << "You choose to draw a Box with an X inside!!!" << endl;
do
{
cout << "Enter an odd number greater than 4 for the size: ";
cin >> size2;
}while(size2<5);
cout <<endl;

for (int i=0; i<=size2; i++) cout << "*";
cout <<endl;

size1 = size2 - 1;

for (int totalRows = 1; totalRows <= size1; totalRows++) {
cout << "*";

for (int totalCols = 1; totalCols <= size1; totalCols++)
{
if (totalRows == totalCols || totalCols == (size1 + 1) -
totalRows) cout << "*";
else cout << " ";
}
cout << "*\n";
}
for (int a=0; a<=size2; a++) cout << "*";
}

else if(shape == 4)
{
	cout << "You choose to draw a Circle!!!" << endl;
	do
	{
   cout << "Enter a number greater than 1 for the radius: " << endl;
   cin>> circle_radius;
	}while(circle_radius<=1);

for (int c = 0; c <= 2*circle_radius; c++)
{
	for (int j = 0; j <= 2*circle_radius; j++)
	{
     	float distance_to_centre = sqrt((c - circle_radius)*(c - circle_radius) + (j - circle_radius)*(j - circle_radius));
    	if (distance_to_centre > circle_radius-0.5 && distance_to_centre < circle_radius+0.5)
    	{
         	cout << "*";
    	}
    	else
    	{
         	cout << " ";
    	}
	}
	cout << endl;
}
}

else if(shape == 5)
{
	cout << "You choose to draw a X!!!" << endl;
	do
{
 cout<< "Enter a number at least 3 for the size: ";
 cin>> number;
}while(number<3);
	for( int rows=1; rows<=number; rows++)
	{
    	for (int cols=1; cols<=number; cols++)
    	{
        	if(rows==cols || cols==(number+1)-rows)
            	cout << "*";
        	else
            	cout << " ";
    	}
    	cout << endl;
	}
}

printmessage ();
return 0;
}
You can take all the code in main, put it in a function and most of the time it works pretty good. you may have to move/fix some variable variable declarations.


#include <iostream>
#include <math.h>
#include <iomanip>
#include <string>
#include <cstdlib>

using namespace std;
int myfunction ()
{
// code here
}

int main ()
{
myfunction();
return 0;
}
Topic archived. No new replies allowed.