Making a square frame out of asterisks

I've almost finished my code for this, but for some reason the output is like this:
**********
* *
**********
* *
**********
* *
**********
and I need the inside to be completely hollow. I've been playing around with the code trying to fix it but only made it worse. Could I get some help?

1
2
3
4
5
6
7
8
9
10
11
  }else if(shape == 3){
    cout << "\nWidth?: ";
    cin >> width;
    cout << "\nHeight?: ";
    cin >> height;
    cout << string(width,'*') << endl;
    for(int a = 0;a < height - 2;++a){
      cout << '*' << string(width - 2, ' ') << '*' << endl;
      cout << string(width, '*') << endl;
    }
    cout << endl << endl;
Your loop needs work. Try this code.

1
2
for (int a = 1; a <= height; a++)
		cout << (a % 2 == 0 ? string(width, ' ') : string(width, '*')) << endl;
@stealthboy

Try it like this..

1
2
3
4
5
6
7
8
9
10
11
12
13
}else if(shape == 3){
    cout << "\nWidth?: ";
	cin >> width;
	string spaces(width - 2, ' ');
	cout << "\nHeight?: ";
	cin >> height;
	cout << string(width, '*') << endl;
	for (int a = 0; a < height - 2; ++a)
		cout << '*' << spaces << '*' << endl;
	cout << string(width, '*') << endl;
	cout << endl << endl;
    }
    cout << endl << endl;
Last edited on
@whitenite1 Your code gave me this
Which shape should I draw?: 3

Width?: 10

Height?: 5
**********
* *
**********

* *
**********

* *
**********

* *
**********

* *
**********
@Stalker Your code gave me
Which shape should I draw?: 3

Width?: 10

Height?: 5
**********
**********

**********

**********
@stealthboy

Did you use the code I posted earlier, or the one shown now? I removed a bracket you had at the end of line 7 in your code. I hadn't noticed it before posting my old code, so I re-posted a corrected version. With the bracket, lines 8 AND 9 of your code is couted, giving you those results you posted. Removing it, will only cout line 8 ( your code ) or line 9 ( my code ) as many times as the a loop shows, THEN couts the bottom line. Try using the code I have posted now, and see if you get the correct output.
Thank you guys for the help, but I think I figured it out. This code I'm using now seems to be working:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  }else if(shape == 3){
    cout << "\nWidth?: ";
    cin >> width;
    cout << "\nHeight?: ";
    cin >> height;
    for(int a = 0;a < height;a++){
      for(int b = 0;b < width;b++){
      if(a == 0||a == height - 1||b == 0||b == width - 1){
        cout << "*";
      }else{
          cout << " ";
      }
    }
    cout << endl;
    }
That is the output I am getting with width = 10, height = 5.

**********

**********

**********
Topic archived. No new replies allowed.