Centering output of a function

I have created a program that has a header file and a .cpp which creates boxes with border characters and fill characters according to what driver my professor runs with my code.

I need to find a way to center these boxes in field of 40 spaces and am having trouble writing this part of the code.

my header file is

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
#ifndef box_h
#define box_h

class Box {

public:
    Box(const int Boxsize, const char fillchar, const char borderchar);
    void SetBorder(const char borderchar);
    void SetFill(const char fillchar);
    void Draw();
    void Grow();
    void Swap(int a, int b); 
    int Getsize();
    int Perimeter();
    void Shrink();
    int Area();
    void Summary();
    
private:
    char fill;
    char border;
    int size;
    

};
#endif 



my .cpp file is

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
#include <iostream>
#include <cmath>
#include "box.h"
#include <string>
#include <sstream>

using namespace std;
int Maxlinesize=40;
void Swap(int a, int b); 
int a;
int b;



/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 ~This is the constructor. It sets the size and the border and fill characters ~
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
Box::Box(const int Boxsize, const char fillchar, const char borderchar)
{
    if(Boxsize< 1) 
    {
        size = 1;
    } 
    else if(Boxsize > 39) 
    {
        size = 39;
    }
    else
    {
        size = Boxsize;
    }
    
    SetBorder(borderchar);
    SetFill(fillchar);
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 ~Here you set the private Size variable. Size is between 1 and 39.              ~
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
int Box::Getsize()
{
    return size;    
    
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 ~Here we calculate the perimeter of the box.                                  ~
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
int Box::Perimeter()
{
    return size*4;
    
    
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 ~Here we calculate the area of the box.                                          ~
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
int Box::Area()
{
    return size*size;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 ~Increase the private Size variable by 1 if the box stays under size 39.      ~
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void Box::Grow()
{
    if(size<39) 
    {
        size++;
        
        
    }
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 ~Decrease the private Size variable by 1 if the box stays over size 1.          ~
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void Box::Shrink()
{
    if (size>1) size--;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 ~Set the private border character variable. Defaults to #                      ~
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void Box::SetBorder(const char borderchar)
{
    switch(borderchar)
    {
        case '~': border='~'; break;
        case '!': border='!'; break;
        case '@': border='@'; break;
        case '$': border='$'; break;
        case '%': border='%'; break;
        case '^': border='^'; break;
        case '&': border='&'; break;
        case '*': border='*'; break;
        case '<': border='<'; break;
        case '>': border='>'; break;
        default: border='#'; break;
    }
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 ~Set the private fill character variable. Defaults to *                          ~
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void Box::SetFill(const char fillchar)
{
    switch (fillchar)
    {
        case '~': fill='~'; break;
        case '!': fill='!'; break;
        case '@': fill='@'; break;
        case '#': fill='#'; break;
        case '$': fill='$'; break;
        case '%': fill='%'; break;
        case '^': fill='^'; break;
        case '&': fill='&'; break;
        case '<': fill='<'; break;
        case '>': fill='>'; break;
        default: fill='*'; break;
    }
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 ~Draw the requested box. The first and last lines consist of only the border. ~
 ~The in between lines have a border character, then fill characters, and a    ~
 ~final border character. Sizes 1 and 2 don't use borders.                      ~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void Box::Swap(int a, int b)
{  
    
    a = border;
    b = fill;
    
    Swap(b, a);
    
    }

void Box::Draw()
{
    cout << endl;
    if (size==1){ cout<< ' '<< border <<' '<<endl; return; }
    if (size==2){ cout<< ' '<< border <<' '<< border<<' '<<endl;
        cout<< ' '<< border <<' '<< border<<' '<<endl; return;}
    
    for (int i=1; i<=size; i++) 
        cout << ' '<<border;
    cout << ' '<<endl;
    
    for (int j=1; j<=size-2; j++)
    {
        cout << ' ' <<border;
        for (int i=2; i<=size-1; i++) 
            cout << ' ' <<fill;
        cout << ' '<<border;
        cout << ' '<<endl;
    }
    for (int i=1; i<=size; i++) 
        cout << ' '<<border;
    cout << ' '<<endl;
    
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 ~This gives a summary showing the box size, perimeter, and area of the box.   ~
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void Box::Summary()
{
    cout <<"The box size is "<< Getsize() << endl;
    cout <<"The perimeter is "<< Perimeter() << endl;
    cout <<"The area is "<< Area() << endl;
}

void Swap(int a, int b) 
{ 
    int temp; 
    
    temp = a;  
    a = b;     
    b = temp;  
}


and the .cpp file my professor uses to test the program is

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
#include <iostream>
#include "box.h" 

using namespace std;

int main(void)
{  
    
    Box mybox1(1,'*','<');  
    Box mybox2(2,'^','!');
    Box mybox10(10,'#','@'); 
    Box mybox39(39, '*','&'); 
    
    
    cout << "Draw a box of size 1, with * border and < fill" << endl; 
    mybox1.Draw();
    
    
    cout << "Try to shrink Box1, see if it stays at 1, Print Summary " << endl;
    mybox1.Shrink();
    mybox1.Summary();
    
    
    
    cout << "Try to change Border to a space, make sure it goes back to default of #" << endl;
    mybox1.SetBorder(' ');
    mybox1.Draw(); 
    
    
    cout << "Draw a box of size 2, with ^ border and ! fill" << endl;  
    mybox2.Draw();
    mybox2.Shrink();
    
    
    cout << "Shrink Box of Size 2, see if it goes to 1,Print Summary" << endl;
    mybox2.Summary(); 
    
    
    cout << "Draw a box of size 10, with # border and @ fill" << endl;   
    mybox10.Draw();
    
    
    cout << "Grow Box of size 10, see if it goes to 11, Print Summary" << endl;
    mybox10.Grow();
    mybox10.Summary();
    
    
    cout<< "Try to set the fill character to a space, make sure it goes to the default of *" << endl;
    mybox10.SetFill(' ');
    mybox10.Draw(); 
    
    
    cout << "Draw a box of size 39, with * border and & fill" << endl;
    mybox39.Draw();
    
    
    cout << "Grow a box of size 39, see if it stays at 39, print summary"<< endl;
    mybox39.Grow();
    mybox39.Summary();
    
    
    return 0;
}


the output i get works perfectly BUT I NEED THE BOXES CENTER WITHIN 40 characters please help!!!

40 / 2 - ( boxSize / 2 ) ?

I use '40 / 2' because then if it needs to be changed, you could replace 40 with a variable. If of course, you use this more than once.

On another note, this function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void Box::SetBorder(const char borderchar)
{
    switch(borderchar)
    {
        case '~': border='~'; break;
        case '!': border='!'; break;
        case '@': border='@'; break;
        case '$': border='$'; break;
        case '%': border='%'; break;
        case '^': border='^'; break;
        case '&': border='&'; break;
        case '*': border='*'; break;
        case '<': border='<'; break;
        case '>': border='>'; break;
        default: border='#'; break;
    }
}


Couldn't you just do:
1
2
3
4
void Box::SetBorder(const char borderchar)
{
        border = borderchar;
}
Last edited on
how would i incorporate that code "40 / 2 - ( boxSize / 2 )" into my draw function?
40 / 2 - ( boxSize / 2 ) Will give you the left side of the boxes position.

So store this in a variable and do something like the following:

1
2
3
4
5
6
7
8
int screenWidth = 40; //this is what I was on about earlier,
                      //when using more than once. in case your
                      //screen size changes

int center = screenWidth / 2 - ( boxSize / 2 );

for( int i = center; i >= 0; --i )
    cout << ' ';


printing spaces will align the left side of the box, so that when the box is fully printed, it will be centered.
I understand but am having dificulty placing it in my code... could you please show me where exactly i should put the code you provided?
You're going to need it in 3 places, take a look at the following code:
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
void align( int center )
{
	for( int i = center; i > 0; --i )
			std::cout << ' ';
}


int main()
{
	char border = '@';
	char fill = '*';
	int size = 20;

	int screenWidth = 80;
	int center = screenWidth / 2 - ( size / 2 );
    
	align( center );

	for ( int i = 1; i <= size; ++i ) 
		std::cout << border;

	std::cout << ' ' << std::endl;
    
	for ( int j = 1; j <= size - 2; ++j )
	{
		align( center );
		std::cout << border;
		
		for ( int i = 2; i <= size - 1; ++i ) 
			std::cout << fill;

		std::cout << border;
		std::cout << ' ' << std::endl;
	}

	align( center );
	for ( int i = 1; i <= size; ++i ) 
		std::cout << border;

	std::cout << std::endl;

	return 0;
}


This is your code, but modified. I took out the spaces within the box.
You need to align the box at the top and bottom border and each loop of the run of the left side.
It makes sense now but when i tried putting this into my .cpp file with the rest of the code it didnt seem to work could you please incorporate it all into one running .cpp please?
i Put my original code below so if you could please put your code with mine that would be awesome!!

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
#include <iostream>
#include <cmath>
#include "box.h"
#include <string>
#include <sstream>

using namespace std;
int Maxlinesize=40;
void Swap(int a, int b); 
int a;
int b;



/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 ~This is the constructor. It sets the size and the border and fill characters ~
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
Box::Box(const int Boxsize, const char fillchar, const char borderchar)
{
    if(Boxsize< 1) 
    {
        size = 1;
    } 
    else if(Boxsize > 39) 
    {
        size = 39;
    }
    else
    {
        size = Boxsize;
    }
    
    SetBorder(borderchar);
    SetFill(fillchar);
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 ~Here you set the private Size variable. Size is between 1 and 39.              ~
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
int Box::Getsize()
{
    return size;    
    
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 ~Here we calculate the perimeter of the box.                                  ~
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
int Box::Perimeter()
{
    return size*4;
    
    
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 ~Here we calculate the area of the box.                                          ~
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
int Box::Area()
{
    return size*size;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 ~Increase the private Size variable by 1 if the box stays under size 39.      ~
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void Box::Grow()
{
    if(size<39) 
    {
        size++;
        
        
    }
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 ~Decrease the private Size variable by 1 if the box stays over size 1.          ~
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void Box::Shrink()
{
    if (size>1) size--;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 ~Set the private border character variable. Defaults to #                      ~
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void Box::SetBorder(const char borderchar)
{
    switch(borderchar)
    {
        case '~': border='~'; break;
        case '!': border='!'; break;
        case '@': border='@'; break;
        case '$': border='$'; break;
        case '%': border='%'; break;
        case '^': border='^'; break;
        case '&': border='&'; break;
        case '*': border='*'; break;
        case '<': border='<'; break;
        case '>': border='>'; break;
        default: border='#'; break;
    }
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 ~Set the private fill character variable. Defaults to *                          ~
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void Box::SetFill(const char fillchar)
{
    switch (fillchar)
    {
        case '~': fill='~'; break;
        case '!': fill='!'; break;
        case '@': fill='@'; break;
        case '#': fill='#'; break;
        case '$': fill='$'; break;
        case '%': fill='%'; break;
        case '^': fill='^'; break;
        case '&': fill='&'; break;
        case '<': fill='<'; break;
        case '>': fill='>'; break;
        default: fill='*'; break;
    }
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 ~Draw the requested box. The first and last lines consist of only the border. ~
 ~The in between lines have a border character, then fill characters, and a    ~
 ~final border character. Sizes 1 and 2 don't use borders.                      ~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void Box::Swap(int a, int b)
{  
    
    a = border;
    b = fill;
    
    Swap(b, a);
    
    }

void Box::Draw()
{
    cout << endl;
    if (size==1){ cout<< ' '<< border <<' '<<endl; return; }
    if (size==2){ cout<< ' '<< border <<' '<< border<<' '<<endl;
        cout<< ' '<< border <<' '<< border<<' '<<endl; return;}
    
    for (int i=1; i<=size; i++) 
        cout << ' '<<border;
    cout << ' '<<endl;
    
    for (int j=1; j<=size-2; j++)
    {
        cout << ' ' <<border;
        for (int i=2; i<=size-1; i++) 
            cout << ' ' <<fill;
        cout << ' '<<border;
        cout << ' '<<endl;
    }
    for (int i=1; i<=size; i++) 
        cout << ' '<<border;
    cout << ' '<<endl;
    
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 ~This gives a summary showing the box size, perimeter, and area of the box.   ~
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void Box::Summary()
{
    cout <<"The box size is "<< Getsize() << endl;
    cout <<"The perimeter is "<< Perimeter() << endl;
    cout <<"The area is "<< Area() << endl;
}

void Swap(int a, int b) 
{ 
    int temp; 
    
    temp = a;  
    a = b;     
    b = temp;  
}


Last edited on
I'm sorry, I don't/can't do peoples homework!

If I do your homework;
1. You won't fully understand what/why I did, and why I did it.
2. When you're asked to do something similar at a later date, you'll be lost again!

I gave you the answer in my last post.

You're function void Box::Draw() is what I have posted...
Last edited on
Topic archived. No new replies allowed.