Need assistance with this program!! pls

I must draw a box using *
1
2
3
4
5
6
7
8
9
*******
*******
**   **
**   **
**   **
**   **
*******
*******


Write a void function drawframe that draws such a figure on the screen. There should be one value parameter (of type int) indicating the width. In the above example the width is 7. Use the main function given below and input a value of 8 as the width.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

//The required function drawframe should be inserted here
void drawFrame(int n)            // ive allready add this to the program
{
    for (int i = 1; i <= n; i++)
        cout << '*';
        cout << endl;
}                               // end of what ive inserted

int main( )
{
    int width;
    
    cout << "What width (greater than 4) frame would you like to draw? ";
    cin >> width;
    cout << endl;
    drawFrame(width);    
    cout << endl << endl;
    return 0;
}


the problem i got is that when i put the width in it should draw the figure but i only get one line of *. So basically i should input one number lets say 8 and it should draw the figure 8 * long and height of 8 *

Please help if you have any ideas.
Last edited on
This is quite straight forward.
Let's look at how the box is made up.
It has two full lines of characters at the top and at the bottom, and the bit between has two chars at the start and two chars at the end with spaces in between.

Let's say the width is X characters, this also happens to be the number of lines height. (int width =X)

We can see that the number of spaces in each line is X-4
The middle part is X-4 lines high

The general code procedure would look something like this.


1. Draw a full line of chars + newline (you know how to do that)

2. Draw another full line of chars + newline

1
2
int numSpaces = width-4;
int middleHeight = width-4; 


3. Now a for loop equal to the middleHeight
for (int count=0; count < middleHeight; count++)
{
3.1 draw two chars at the start of the line
3.2 now an inner for loop for the spaces for (int spaceCount=0; spaceCount < numSpaces; spaceCount++)
{
loop and Draw spaces
}//end space loop
3.3 Draw two chars to end the line + newline

3.4 } //End middleHeight loop

4. Draw a full line of chars + carriage return
5. Draw another full line of chars









Topic archived. No new replies allowed.