Void Functions

I need help writing a program that asks the user for an integer, then should call a void function, passing that integer to it, and producing a box using the integer as the length of the sides. The function will need to output the box in three parts: (a) the top row of asterisks, (b) the middle section, and (c) the bottom row of asterisks. The middle section also requires three parts: (a) the first asterisk, (b) the spaces in the middle, and (c) the last asterisk.

Any help will be appreciated!
what have you written?
closed account (3qX21hU5)
Here is a template to get your started. I left the hardest parts for you :) since it is a good learning experience. Just go through it a step at a time. If you have any questions just let us know but make sure you are specific on what you are having trouble with and post a detailed question.

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
#include <iostream>
#include <string>
#include <sstream>  // Need this for stringstream

using namespace std;

void printBox(const int sizeOfBox);

int main()
{
    //Get all the user input here
    
    printBox(number);
}

void printBox(const int sizeOfBox)
{
    // This is your top row of asterisks
    cout << string(sizeOfBox, '*') << endl;
    
    // Print the middle row here. Hint: You will need nested loops.
    
    // This will print the bottom row
    cout << string(sizeOfBox, '*') << endl;
}
This is what Ive changed but Im not sure where to go from here.

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
#include <iostream>
#include <string>
#include <sstream>  

using namespace std;

void printBox(const int sizeOfBox);
int number;

int main()
{
    cout << "Enter a number: ";
	cin >> number;
	

    
    printBox(number);
	return 0;
}

void printBox(const int sizeOfBox)
{
    
    cout << string(sizeOfBox, '*') << endl;
    
 for (int row = 0; row<number; row++)
 {
  for (int column = 0; column < number; column++)
  cout<<"* ";

  cout<<endl;
 }
 
     cout << string(sizeOfBox, '*') << endl;
}
 

closed account (3qX21hU5)
Also you are using a global variable for number... Why? That defeats the purpose of functions. Move that int number; inside of main() and use the sizeOfBox parameter in your function.


Ok as of right now you are filling the middle of the square with *'s.

Lets take a look at how it should look if the user enters the number 5 for example

*****
*   *
*   *   (More like a rectangle but it works for now.)
*   *
*****


Now we already got the top and the bottom done. So we can ignore that. What we need to do it take care of the middle 3 asterisks on each side and the spaces in the middle.

I made a mistake by telling you that you needed to have nested loops, sorry about that. We actually only need to use one loop that will print out each row of the box. Though we need to make a little change to the loop like so.
for (int row = 0; row < sizeOfBox - 2; row++)

The reason why we minus 2 from the max number is because the top and bottom rows make up 2 rows already. So no matter what number the user enters the middle part will always be 2 rows less then that.


Next we need to take a look at the cout statement. Since we are printing a whole row at a time it is much easier to figure out. All you need to do is print the first '*' then the correct number of spaces in the middle and the ending '*'.

Hopefully this helps, not sure if I explained it that well.


Hint: After you figure out how to make a rectangle you might need to change it so that it prints a square instead depending on how your assignment is worded. To do this you are going to have to change the parts that print the top and bottom part of the box.
Last edited on
So this is what I have and when it outputs it is missing the asterisks that go in between the last column.

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
#include <iostream>
#include <string>
#include <sstream>  

using namespace std;

void printBox(const int sizeOfBox);


int main()
{
	int number;
    cout << "Enter a number: ";
	cin >> number;
	printBox(number);
	return 0;
}

void printBox(const int sizeOfBox)
{
    
    cout << string(sizeOfBox, '*') << endl;
    
 
	for (int row = 0; row < sizeOfBox - 2; row++)
	{
		cout<<"* ";
		cout<<endl;
	}
 
     cout << string(sizeOfBox, '*') << endl;
}
 
Figured it out!

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
#include <iostream> 

using namespace std; 
void box(int n) 
{ 
	for(int i =0;i<n;i++) 
	{ 

		if(i == 0 || i == n-1)
		{ 
			for(int j=0;j<n;j++) 
				cout<<"*"; 
				cout<<endl; 
		} 

		else
		{ 
			cout<<"*"; 

			for(int j=1;j<n-1;j++) 
			cout<<" "; 
			cout<<"*"<<endl;
		} 
	} 

} 

int main() 
{ 
	int n; 
	cout<<"Enter any positive integer :";
	cin>>n; 
	box(n); 
} 
closed account (3qX21hU5)
Very nice! looks good another way to do it would be to just print it row by row.

For example if the user enters the number 5 we know the first row and bottom row will look like this.

*****


We also know each middle row will look like this

*   *

(1 * on either side with 3 spaces in the middle.

To recap it goes like this.

top row = userNumber of *'s (User number equals whatever number the user enters.
middle rows = * first then (userNumber - 2) of spaces and then the ending *
bottom row = userNumber of *'s

So since we always know what every row will look like we can just use a loop to print every row with whatever size the user determines. Which is what you did, but we can simplify it a little bit. Like so.

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
#include <iostream>
#include <string>

using namespace std;

void printBox(const int sizeOfBox);

int main()
{
    int number;
    cout << "Enter a number: ";
	cin >> number;



    printBox(number);
	return 0;
}

void printBox(const int sizeOfBox)
{

    cout << string(sizeOfBox, '*') << endl;

    for (int row = 0; row < sizeOfBox - 2; row++)
    {
        cout << "*" << string(sizeOfBox - 2, ' ') << "*" << endl;
    }

     cout << string(sizeOfBox, '*') << endl;
}


But either way works just fine. Anyways nice job on the assignment if you have trouble with anything else or got anymore questions I would be glad to help out.
Topic archived. No new replies allowed.