Help me draw a box, please.

I've spent some time researching how to draw a box with asterisks and have yet to come up with a solution.

My problem is I can get my asterisks to either vertically or horizontally but not both at the same time to draw my box as depicted below.

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

Hint: you can use either the % operator OR use an extra counting variable.

My code...

case '2':cout<<"Please enter the height of the rectangle ";
cout<<"(1-10): ";
cin>>height;

while(height<1||height>10)
{
cout<<"\nInvalid input. Please re-enter the height ";
cout<<"of the rectangle. ";
cin>>height;
}

cout<<"\nPlease enter the width of the rectangle ";
cout<<"(1-10): ";
cin>>width;

while(width<1||width>10)
{
cout<<"\nInvalid input. Please re-enter the width ";
cout<<"of the rectangle. ";
cin>>width;
}

do
{
cout<<"\n*";
cnt++;
}while(cnt<=width);
break;
change to do while loop . example
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>

using namespace std;


int main(){
	int height = 0; //Initialize the value , it's important
	int width  = 0;
	do{
	cout << "Enter Height : ";
	cin >> height;
        if( height<1 || height > 10 )
		cout << "Invalid Input for height ! " << endl << endl;
	}while(height<1 || height > 10 );

	do{
	cout << "Enter width: ";
	cin >> width;
        if( width<1 || width > 10 )
		cout << "Invalid Input for width ! " << endl << endl;
	}while(width<1 || width > 10 );

	for( int i = 0 ; i < height ; i++ ){
		for( int j = 0 ; j < width ; j++ ){
			cout << "*" ;
		}
		cout << endl;
	}
	system( "pause" );
	return 0;
}


Not really sure it's the code work or not. please check yourself . and try to understand for the looping your concept not wrong. but the way you implementation was wrong.
Last edited on
Topic archived. No new replies allowed.