Magic Square with boxes

I badly need help for my assignment which i due tomorrow. Please help me out!

So the task is to create a magic square wherein there is actually a box so each number would be in a cell. And I got mine working already but my problem is once the user enters any two-digit number above, the box kinda gets messy so the lines are no longer in place and the box isn't a box anymore. What is the correct algorithm???

Here's what I got so far:

#include <iostream>
#include <iomanip>
#include <windows.h>
#include "ecpe202.h"

void box(int n);

using namespace std;

int main()
{
//ask user for size
int n,x,y;

while (n%2 == 0 || n == 1){
cout << "===== MAGIC SQUARE SIMULATOR ====\n\n" << endl;
cout << "Enter an odd number for the size of the Magic Square: ";
cin >> n;
if (n%2 == 0 || n == 1){
cout << "\n\n**** Invalid input. Enter an odd number greater than 1.!!! **** " << "\n";
}
}
clrscr();
//create an array and place 0s in it
int magicSquare[n][n];
for (int i = 0; i < n; i++){
for (int z = 0; z < n; z++){
magicSquare[i][z] = 0;
}
}
//determine first position
int i = 0;
int z = (n - 1)/2;

//first position
magicSquare[i][z] = 1;

int key = 2;
int b, c;

//determining the positions ( from 2 to n*n )
while(key <= n * n ){

//move up and left
b = (i - 1) % n;
c = (z - 1) % n;

// if row is out of bounds
if( b < 0 && c >= 0 ){
i = n - 1;
z = (z - 1)%n;
}

// if col is out of bounds
else if( c < 0 && b >= 0){
z = n - 1;
i = (i - 1) % n;
}

else if( b < 0 && c < 0 ){
i = (i + 1)%n;
}
// if space is occupied, move down
else if(magicSquare[b][c] != 0){
i = (i + 1)%n;
}

//assign b,c to i,z
else {
i = b;
z = c;
//magicSquare[i][z] = key;
}

//put the value in
magicSquare[i][z] = key;
key++;
}
//int common;
box(n);
for (int x = 0; x < n; x++){
for (int y = 0; y < n; y++){
if (n*n < 10){
gotoxy (y*2+1, x * 2 + 1); cout<< magicSquare[x][y];
}else if (n*n<100){
gotoxy (y*3+1,x*2 + 1);cout <<magicSquare [x][y];
}else{
gotoxy (y*4+1,x*2+1); cout <<magicSquare [x][y];
}
}
// common == magicSquare [0][x];
}
// gotoxy (0,n*2+2);cout<< "where the common sum is "<< common << endl;


cout << "\n\nSum of each vertical line: "<< setw(5) << n*(n*n+1)/2 << endl;
cout << "Sum of each horizontal line: " << setw(5) << n*(n*n+1)/2 << endl;
cout << "Sum of each diagonal line: " << setw(3) << n*(n*n+1)/2 <<endl;


/* for (i = 0; i < n; i++){
for (j = 0; j < n; j++){
cout << "|" << setw(5)<<magicSquare[i][j]<<" ";
}
cout<<endl;
} */
/* cout << "Solve for another? Y or N?";
char a;
cin >> a;
if (a == 'y','Y'){
return main();
}
else { */
cout << "\n" <<endl;
return main();
}


void box (int n) {
if (n*n < 10) {
for (int i = 0; i < n * 2 + 1; i++) {
for (int z = 0; z < n * 2 + 1; z++) {
if (i == 0) {
if (z == 0) {
cout << '\xDA';
}
if (z%2 == 1 && z != n * 2 + 1) {
cout << '\xC4';
}
if (z%2 == 0 && z != n * 2 && z != 0) {
cout << '\xC2';
}
if (z == n*2){
cout << '\xBF';
}
}
if (i%2 == 1 && i != n*2 + 1) {
if (z%2 == 1) {
cout << " ";
}else {
cout << '\xB3';
}
}
if (i%2 == 0 && i != 0 && i != n*2) {
if (z == 0) {
cout << '\xC3';
}
if (z%2 == 1 && z != n*2 + 1) {
cout << '\xC4';
}
if (z%2 == 0 && z != n * 2 && z != 0) {
cout << '\xC5';
}
if (z == n*2){
cout << '\xB4';
}
}
if (i == n*2){
if (z == 0) {
cout << '\xC0';
}
if (z%2 == 1 && z != n*2 + 1) {
cout << '\xC4';
}
if (z%2 == 0 && z != n * 2 && z != 0) {
cout << '\xC1';
}
if (z == n*2){
cout << '\xD9';
}
}
}
cout << endl;
}
} else if (n*n < 100) {
for (int i = 0; i < n * 2 + 1; i++) {
for (int z = 0; z < n * 2 + 1; z++) {
if (i == 0) {
if (z == 0) {
cout << '\xDA';
}
if (z%2 == 1 && z != n * 2 + 1) {
cout << '\xC4' << '\xC4';
}
if (z%2 == 0 && z != n * 2 && z != 0) {
cout << '\xC2';
}
if (z == n*2){
cout << '\xBF';
}
}
if (i%2 == 1 && i != n*2 + 1) {
if (z%2 == 1) {
cout << " ";
}else {
cout << '\xB3';
}
}
if (i%2 == 0 && i != 0 && i != n*2) {
if (z == 0) {
cout << '\xC3';
}
if (z%2 == 1 && z != n*2 + 1) {
cout << '\xC4' << '\xC4';
}
if (z%2 == 0 && z != n * 2 && z != 0) {
cout << '\xC5';
}
if (z == n*2){
cout << '\xB4';
}
}
if (i == n*2){
if (z == 0) {
cout << '\xC0';
}
if (z%2 == 1 && z != n*2 + 1) {
cout << '\xC4' << '\xC4';
}
if (z%2 == 0 && z != n * 2 && z != 0) {
cout << '\xC1';
}
if (z == n*2){
cout << '\xD9';
}
}
}
cout << endl;
}
} else {
for (int i = 0; i < n * 2 + 1; i++) {
for (int z = 0; z < n * 2 + 1; z++) {
if (i == 0) {
if (z == 0) {
cout << '\xDA';
}
if (z%2 == 1 && z != n * 2 + 1) {
cout << '\xC4' << '\xC4' << '\xC4';
}
if (z%2 == 0 && z != n * 2 && z != 0) {
cout << '\xC2';
}
if (z == n*2){
cout << '\xBF';
}
}
if (i%2 == 1 && i != n*2 + 1) {
if (z%2 == 1) {
cout << " ";
}else {
cout << '\xB3';
}
}
if (i%2 == 0 && i != 0 && i != n*2) {
if (z == 0) {
cout << '\xC3';
}
if (z%2 == 1 && z != n*2 + 1) {
cout << '\xC4' << '\xC4' << '\xC4';
}
if (z%2 == 0 && z != n * 2 && z !=0){

cout << '\xC5';
}
}
}
}
}
}
and here are the codes for the library "ecpe202.h" which was used above.


#include <windows.h>

#define FOREVER true
#define MAXSTACK 100
#define MAXQUEUE 100

using namespace std;

struct myStack{
int tos;
char s[MAXSTACK];
};

struct myQueue {
int head;
int tail;
int q[MAXQUEUE];
};

void createS(myStack &S){
S.tos=-1;
}

void pushS(myStack &S,char item){
S.s[++S.tos]=item;
}
char popS(myStack&S){
return(S.s[S.tos--]);
}

bool isFullS(myStack S){
if(S.tos == MAXSTACK-1)
return (true);
return false;
}

bool isEmptyS(myStack S){
if(S.tos == -1)
return true;
return false;
}
//end of stack

void createQ(myQueue&Q){
Q.head=0;
Q.tail=0;
}
void addQ(myQueue&Q, int item){
Q.q[Q.tail++]=item;
Q.tail%=MAXQUEUE;
}
int retrieveQ(myQueue&Q){
int temp;
temp = Q.q[Q.head++];
Q.head%=MAXQUEUE;
return (temp);
}
bool isFullQ(myQueue Q){
if(Q.tail == MAXQUEUE)
return (true);
return false;
}
bool isEmptyQ(myQueue Q){
if(Q.tail == Q.head)
return(true);
return false;
}
void gotoxy(int x,int y){
COORD coord;
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
void clrscr(){
system("CLS");
}
http://www.cplusplus.com/articles/jEywvCM9/
Looks like this
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#include <iostream>
#include <iomanip>
#include <windows.h>
#include "ecpe202.h"

void box(int n);

using namespace std;

int main()
{
//ask user for size
  int n, x, y;

  while (n % 2 == 0 || n == 1) {
    cout << "===== MAGIC SQUARE SIMULATOR ====\n\n" << endl;
    cout << "Enter an odd number for the size of the Magic Square: ";
    cin >> n;
    if (n % 2 == 0 || n == 1) {
      cout << "\n\n**** Invalid input. Enter an odd number greater than 1.!!! **** " << "\n";
    }
  }
  clrscr();

  //create an array and place 0s in it
  int magicSquare[n][n];
  for (int i = 0; i < n; i++) {
    for (int z = 0; z < n; z++) {
      magicSquare[i][z] = 0;
    }
  }

  //determine first position
  int i = 0;
  int z = (n - 1) / 2;

  //first position
  magicSquare[i][z] = 1;

  int key = 2;
  int b, c;

  //determining the positions ( from 2 to n*n )
  while (key <= n * n) {

    //move up and left
    b = (i - 1) % n;
    c = (z - 1) % n;

    // if row is out of bounds
    if (b < 0 && c >= 0) {
      i = n - 1;
      z = (z - 1) % n;
    }
    // if col is out of bounds
    else if (c < 0 && b >= 0) {
      z = n - 1;
      i = (i - 1) % n;
    }

    else if (b < 0 && c < 0) {
      i = (i + 1) % n;
    }
    // if space is occupied, move down
    else if (magicSquare[b][c] != 0) {
      i = (i + 1) % n;
    }
    //assign b,c to i,z
    else {
      i = b;
      z = c;
      //magicSquare[i][z] = key;
    }

    //put the value in
    magicSquare[i][z] = key;
    key++;
  }

  //int common;
  box(n);
  for (int x = 0; x < n; x++) {
    for (int y = 0; y < n; y++) {
      if (n * n < 10) {
        gotoxy(y * 2 + 1, x * 2 + 1);
        cout << magicSquare[x][y];
      } else if (n * n < 100) {
        gotoxy(y * 3 + 1, x * 2 + 1);
        cout << magicSquare[x][y];
      } else {
        gotoxy(y * 4 + 1, x * 2 + 1);
        cout << magicSquare[x][y];
      }
    }
    // common == magicSquare [0][x];
  }
  // gotoxy (0,n*2+2);cout<< "where the common sum is "<< common << endl;
  cout << "\n\nSum of each vertical line: " << setw(5) << n * (n * n + 1) / 2 << endl;
  cout << "Sum of each horizontal line: " << setw(5) << n * (n * n + 1) / 2 << endl;
  cout << "Sum of each diagonal line: " << setw(3) << n * (n * n + 1) / 2 << endl;

  /* for (i = 0; i < n; i++){
  for (j = 0; j < n; j++){
  cout << "|" << setw(5)<<magicSquare[i][j]<<" ";
  }
  cout<<endl;
  } */
  /* cout << "Solve for another? Y or N?";
  char a;
  cin >> a;
  if (a == 'y','Y'){
  return main();
  }
  else { */
  cout << "\n" << endl;
  return main();
}


void box(int n)
{
  if (n * n < 10) {
    for (int i = 0; i < n * 2 + 1; i++) {
      for (int z = 0; z < n * 2 + 1; z++) {
        if (i == 0) {
          if (z == 0) {
            cout << '\xDA';
          }
          if (z % 2 == 1 && z != n * 2 + 1) {
            cout << '\xC4';
          }
          if (z % 2 == 0 && z != n * 2 && z != 0) {
            cout << '\xC2';
          }
          if (z == n * 2) {
            cout << '\xBF';
          }
        }
        if (i % 2 == 1 && i != n * 2 + 1) {
          if (z % 2 == 1) {
            cout << " ";
          } else {
            cout << '\xB3';
          }
        }
        if (i % 2 == 0 && i != 0 && i != n * 2) {
          if (z == 0) {
            cout << '\xC3';
          }
          if (z % 2 == 1 && z != n * 2 + 1) {
            cout << '\xC4';
          }
          if (z % 2 == 0 && z != n * 2 && z != 0) {
            cout << '\xC5';
          }
          if (z == n * 2) {
            cout << '\xB4';
          }
        }
        if (i == n * 2) {
          if (z == 0) {
            cout << '\xC0';
          }
          if (z % 2 == 1 && z != n * 2 + 1) {
            cout << '\xC4';
          }
          if (z % 2 == 0 && z != n * 2 && z != 0) {
            cout << '\xC1';
          }
          if (z == n * 2) {
            cout << '\xD9';
          }
        }
      }
      cout << endl;
    }
  } else if (n * n < 100) {
    for (int i = 0; i < n * 2 + 1; i++) {
      for (int z = 0; z < n * 2 + 1; z++) {
        if (i == 0) {
          if (z == 0) {
            cout << '\xDA';
          }
          if (z % 2 == 1 && z != n * 2 + 1) {
            cout << '\xC4' << '\xC4';
          }
          if (z % 2 == 0 && z != n * 2 && z != 0) {
            cout << '\xC2';
          }
          if (z == n * 2) {
            cout << '\xBF';
          }
        }
        if (i % 2 == 1 && i != n * 2 + 1) {
          if (z % 2 == 1) {
            cout << " ";
          } else {
            cout << '\xB3';
          }
        }
        if (i % 2 == 0 && i != 0 && i != n * 2) {
          if (z == 0) {
            cout << '\xC3';
          }
          if (z % 2 == 1 && z != n * 2 + 1) {
            cout << '\xC4' << '\xC4';
          }
          if (z % 2 == 0 && z != n * 2 && z != 0) {
            cout << '\xC5';
          }
          if (z == n * 2) {
            cout << '\xB4';
          }
        }
        if (i == n * 2) {
          if (z == 0) {
            cout << '\xC0';
          }
          if (z % 2 == 1 && z != n * 2 + 1) {
            cout << '\xC4' << '\xC4';
          }
          if (z % 2 == 0 && z != n * 2 && z != 0) {
            cout << '\xC1';
          }
          if (z == n * 2) {
            cout << '\xD9';
          }
        }
      }
      cout << endl;
    }
  } else {
    for (int i = 0; i < n * 2 + 1; i++) {
      for (int z = 0; z < n * 2 + 1; z++) {
        if (i == 0) {
          if (z == 0) {
            cout << '\xDA';
          }
          if (z % 2 == 1 && z != n * 2 + 1) {
            cout << '\xC4' << '\xC4' << '\xC4';
          }
          if (z % 2 == 0 && z != n * 2 && z != 0) {
            cout << '\xC2';
          }
          if (z == n * 2) {
            cout << '\xBF';
          }
        }
        if (i % 2 == 1 && i != n * 2 + 1) {
          if (z % 2 == 1) {
            cout << " ";
          } else {
            cout << '\xB3';
          }
        }
        if (i % 2 == 0 && i != 0 && i != n * 2) {
          if (z == 0) {
            cout << '\xC3';
          }
          if (z % 2 == 1 && z != n * 2 + 1) {
            cout << '\xC4' << '\xC4' << '\xC4';
          }
          if (z % 2 == 0 && z != n * 2 && z != 0) {

            cout << '\xC5';
          }
        }
      }
    }
  }
}

///////
#include <windows.h>

#define FOREVER true
#define MAXSTACK 100
#define MAXQUEUE 100

using namespace std;

struct myStack {
  int tos;
  char s[MAXSTACK];
};

struct myQueue {
  int head;
  int tail;
  int q[MAXQUEUE];
};

void createS(myStack & S)
{
  S.tos = -1;
}

void pushS(myStack & S, char item)
{
  S.s[++S.tos] = item;
}

char popS(myStack & S)
{
  return (S.s[S.tos--]);
}

bool isFullS(myStack S)
{
  if (S.tos == MAXSTACK - 1)
    return (true);
  return false;
}

bool isEmptyS(myStack S)
{
  if (S.tos == -1)
    return true;
  return false;
}

//end of stack

void createQ(myQueue & Q)
{
  Q.head = 0;
  Q.tail = 0;
}

void addQ(myQueue & Q, int item)
{
  Q.q[Q.tail++] = item;
  Q.tail %= MAXQUEUE;
}

int retrieveQ(myQueue & Q)
{
  int temp;
  temp = Q.q[Q.head++];
  Q.head %= MAXQUEUE;
  return (temp);
}

bool isFullQ(myQueue Q)
{
  if (Q.tail == MAXQUEUE)
    return (true);
  return false;
}

bool isEmptyQ(myQueue Q)
{
  if (Q.tail == Q.head)
    return (true);
  return false;
}

void gotoxy(int x, int y)
{
  COORD coord;
  coord.X = x;
  coord.Y = y;
  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

void clrscr()
{
  system("CLS");
}


> What is the correct algorithm???
You work out the length of the largest integer you're going to print, then arrange for each cell of your square to have that size.
Any number smaller than that, you pad with leading spaces.

So if your max is say 144, then 3 would be " 3" (or if you're fancy " 3 "), and 42 would be " 42".

No I haven't run your code.

How do I customize the size of the cells of my square? I am currently at a loss
> I badly need help for my assignment which i due tomorrow. Please help me out!
Well the short-term answer is save what you have so that you have at least something to submit, even if it's not ideal.

All non-trivial programs begin with pencil and paper.

Draw a few cells for single digit numbers.
Draw a few cells for two digit numbers.
Draw a few cells for three digit numbers.

Study the relationship between number length and the shape of your cells, and what it takes to draw each cell.

Create prototype programs which you can use to study whether your ideas would work in practice.

What you DON'T want to do is write 100's of lines of code, and then discover the night before that it doesn't work!!!

> How do I customize the size of the cells of my square?
I thought that was what the 'n' parameter to box() was all about.
So anyway, fixed some code.
https://ibb.co/ZTG5TGt

It gets a LOT easier to spot what's missing when you have symbolic constants to represent all the different box elements. You can't see what's missing in a blizzard of hex.
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
#include <iostream>
#include <iomanip>
#include <sstream>

void box(int n);

using namespace std;

int main()
{
//ask user for size
  int n, x, y;

  // Yes,your while loop suffered from using uninitialised n
  do {
    cout << "===== MAGIC SQUARE SIMULATOR ====\n\n" << endl;
    cout << "Enter an odd number for the size of the Magic Square: ";
    cin >> n;
    if (n % 2 == 0 || n == 1) {
      cout << "\n\n**** Invalid input. Enter an odd number greater than 1.!!! **** " << "\n";
    }
  }
  while (n % 2 == 0 || n == 1);
  box(n);
  return 0;
}

#ifdef WINDOWS
#else
// https://en.wikipedia.org/wiki/Box-drawing_character#Unix,_CP/M,_BBS
#define BOX_TL    0x6C
#define BOX_HOR   0x71
#define BOX_VTEE  0x77
#define BOX_TR    0x6B
#define BOX_VER   0x78
#define BOX_LTEE  0x74
#define BOX_RTEE  0x75
#define BOX_BTEE  0x76
#define BOX_CROSS 0x6E
#define BOX_BL    0x6D
#define BOX_BR    0x6A

std::string boxchar(unsigned char b) {
  std::ostringstream os;
  os << "\x1b(0" << b << "\x1b(B";
  return os.str();
}

#endif

void box(int n)
{
  if (n * n < 10) {
    for (int i = 0; i < n * 2 + 1; i++) {
      for (int z = 0; z < n * 2 + 1; z++) {
        if (i == 0) {
          if (z == 0) {
            cout << boxchar(BOX_TL);
          }
          if (z % 2 == 1 && z != n * 2 + 1) {
            cout << boxchar(BOX_HOR);
          }
          if (z % 2 == 0 && z != n * 2 && z != 0) {
            cout << boxchar(BOX_VTEE);
          }
          if (z == n * 2) {
            cout << boxchar(BOX_TR);
          }
        }
        if (i % 2 == 1 && i != n * 2 + 1) {
          if (z % 2 == 1) {
            cout << " ";
          } else {
            cout << boxchar(BOX_VER);
          }
        }
        if (i % 2 == 0 && i != 0 && i != n * 2) {
          if (z == 0) {
            cout << boxchar(BOX_LTEE);
          }
          if (z % 2 == 1 && z != n * 2 + 1) {
            cout << boxchar(BOX_HOR);
          }
          if (z % 2 == 0 && z != n * 2 && z != 0) {
            cout << boxchar(BOX_CROSS);
          }
          if (z == n * 2) {
            cout << boxchar(BOX_RTEE);
          }
        }
        if (i == n * 2) {
          if (z == 0) {
            cout << boxchar(BOX_BL);
          }
          if (z % 2 == 1 && z != n * 2 + 1) {
            cout << boxchar(BOX_HOR);
          }
          if (z % 2 == 0 && z != n * 2 && z != 0) {
            cout << boxchar(BOX_BTEE);
          }
          if (z == n * 2) {
            cout << boxchar(BOX_BR);
          }
        }
      }
      cout << endl;
    }
  } else if (n * n < 100) {
    for (int i = 0; i < n * 2 + 1; i++) {
      for (int z = 0; z < n * 2 + 1; z++) {
        if (i == 0) {
          if (z == 0) {
            cout << boxchar(BOX_TL);
          }
          if (z % 2 == 1 && z != n * 2 + 1) {
            cout << boxchar(BOX_HOR) << boxchar(BOX_HOR);
          }
          if (z % 2 == 0 && z != n * 2 && z != 0) {
            cout << boxchar(BOX_VTEE);
          }
          if (z == n * 2) {
            cout << boxchar(BOX_TR);
          }
        }
        if (i % 2 == 1 && i != n * 2 + 1) {
          if (z % 2 == 1) {
            cout << "  ";
          } else {
            cout << boxchar(BOX_VER);
          }
        }
        if (i % 2 == 0 && i != 0 && i != n * 2) {
          if (z == 0) {
            cout << boxchar(BOX_LTEE);
          }
          if (z % 2 == 1 && z != n * 2 + 1) {
            cout << boxchar(BOX_HOR) << boxchar(BOX_HOR);
          }
          if (z % 2 == 0 && z != n * 2 && z != 0) {
            cout << boxchar(BOX_CROSS);
          }
          if (z == n * 2) {
            cout << boxchar(BOX_RTEE);
          }
        }
        if (i == n * 2) {
          if (z == 0) {
            cout << boxchar(BOX_BL);
          }
          if (z % 2 == 1 && z != n * 2 + 1) {
            cout << boxchar(BOX_HOR) << boxchar(BOX_HOR);
          }
          if (z % 2 == 0 && z != n * 2 && z != 0) {
            cout << boxchar(BOX_BTEE);
          }
          if (z == n * 2) {
            cout << boxchar(BOX_BR);
          }
        }
      }
      cout << endl;
    }
  } else {
    for (int i = 0; i < n * 2 + 1; i++) {
      for (int z = 0; z < n * 2 + 1; z++) {
        if (i == 0) {
          if (z == 0) {
            cout << boxchar(BOX_TL);
          }
          if (z % 2 == 1 && z != n * 2 + 1) {
            cout << boxchar(BOX_HOR) << boxchar(BOX_HOR) << boxchar(BOX_HOR);
          }
          if (z % 2 == 0 && z != n * 2 && z != 0) {
            cout << boxchar(BOX_VTEE);
          }
          if (z == n * 2) {
            cout << boxchar(BOX_TR);
          }
        }
        if (i % 2 == 1 && i != n * 2 + 1) {
          if (z % 2 == 1) {
            cout << "   ";
          } else {
            cout << boxchar(BOX_VER);
          }
        }
        if (i % 2 == 0 && i != 0 && i != n * 2) {
          if (z == 0) {
            cout << boxchar(BOX_LTEE);
          }
          if (z % 2 == 1 && z != n * 2 + 1) {
            cout << boxchar(BOX_HOR) << boxchar(BOX_HOR) << boxchar(BOX_HOR);
          }
          if (z % 2 == 0 && z != n * 2 && z != 0) {
            cout << boxchar(BOX_CROSS);
          }
          if (z == n * 2) { //!! This was missing
            cout << boxchar(BOX_RTEE);
          }
        }
        if (i == n * 2) { //!! All this was missing
          if (z == 0) {
            cout << boxchar(BOX_BL);
          }
          if (z % 2 == 1 && z != n * 2 + 1) {
            cout << boxchar(BOX_HOR) << boxchar(BOX_HOR) << boxchar(BOX_HOR);
          }
          if (z % 2 == 0 && z != n * 2 && z != 0) {
            cout << boxchar(BOX_BTEE);
          }
          if (z == n * 2) {
            cout << boxchar(BOX_BR);
          }
        }
      }
      cout << endl; //!! also missing
    }
  }
}


What you should really be thinking about is that the whole interior of the square is common code comprised of nothing more than
1
2
cout << "  " << boxchar(BOX_VER);
cout << boxchar(BOX_HOR) << boxchar(BOX_HOR) << boxchar(BOX_CROSS);


You could even pre-create the common elements which depend on the value of n.
Then you wouldn't need the if (n * n < 10) if (n * n < 100) if (n * n < 1000) bloat generator.
1
2
3
4
5
6
7
8
int len = (int)log10(n*n) + 1;
std::ostringstream l1, l2;
for ( int i = 0 ; i < len ; i++ ) {
  l1 << " ";
  l2 << boxchar(BOX_HOR);
}
l1 << boxchar(BOX_VER);
l2 << boxchar(BOX_CROSS);



Last edited on
Voila!
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
void box(int n) {
  int len = (int)log10(n*n)+1;

  // Prepare the oft repeated elements
  std::ostringstream hdash, hspace;
  for ( int i = 0 ; i < len ; i++ ) {
    hdash << boxchar(BOX_HOR);
    hspace << " ";
  }

  // Top Row
  cout << boxchar(BOX_TL);
  for ( int c = 0 ; c < n-1 ; c++ ) cout << hdash.str() << boxchar(BOX_VTEE);
  cout << hdash.str() << boxchar(BOX_TR);
  cout << endl;

  // All the cells, except the last row of cells
  for ( int r = 0 ; r < n-1 ; r++ ) {
    // spaces and vertical lines
    cout << boxchar(BOX_VER);
    for ( int c = 0 ; c < n ; c++ ) cout << hspace.str() << boxchar(BOX_VER);
    cout << endl;

    // tee pieces, horizontal lines and crosses
    cout <<  boxchar(BOX_LTEE);
    for ( int c = 0 ; c < n-1 ; c++ ) cout << hdash.str() << boxchar(BOX_CROSS);
    cout << hdash.str() << boxchar(BOX_RTEE);
    cout << endl;
  }

  // Last row of spaces and vertical lines
  cout << boxchar(BOX_VER);
  for ( int c = 0 ; c < n ; c++ ) cout << hspace.str() << boxchar(BOX_VER);
  cout << endl;

  // bottom row
  cout << boxchar(BOX_BL);
  for ( int c = 0 ; c < n-1 ; c++ ) cout << hdash.str() << boxchar(BOX_BTEE);
  cout << hdash.str() << boxchar(BOX_BR);
  cout << endl;
}
Thank you so much!! You're a life saver
Topic archived. No new replies allowed.