Making a box out of asterisks problem

Hello, guys. I am trying to make a hollow box out of asterisks whose size is determined by the user. Check the comments to see what I mean if it's unclear; also, there is the output I'm getting. My IDE is CodeBlocks and my OS is Win7. I'm wondering why my output is off. This was a problem I had a few years ago and now I'm trying to solve it on my own. Any help is appreciated.

The problem is in the makeSideOfBox function.

edit: I solved it on my own. Not the cleanest of solutions. I just added a "cout << endl;" after the first calling of the makeTopOrBot functions. And changed the makeSideOfBox function slightly--not sure if that mattered though.


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

using namespace std;

//expected output with width and length 5:
//*****
//*   *
//*   *
//*   *
//*****
//
//output I'm getting:
//******      *
//*      *
//*      *
//*****



void makeTopOrBot(int width);
void getDimensions(int &width, int &length);
void makeSideOfBox(int width);


int main()
{
    int width;
    int length;
    getDimensions(width, length);
    makeTopOrBot(width);
    for(int x = 0; x < length -2; x++){
    makeSideOfBox(width);
    }
    makeTopOrBot(width);
}

void makeTopOrBot(int width){
    for(int x = 0; x<width; x++){
        cout << "*";
    }

}

void getDimensions(int &width, int &length){
    cout << "Enter width (must be an odd number): ";
    cin >> width;
    cout << "Enter the length: ";
    cin >> length;
}

void makeSideOfBox(int width){
    for(int x= 1; x<=width; x++){
        if(x==1){
            cout << "*";
        }
        if(x != 1 || width){
            cout << " ";
        }
        if(x==width){
            cout << "*";
        }
    }
    cout << endl;
}
Last edited on
Topic archived. No new replies allowed.