-
Box Drawing Program
I'm writing a program that displays a box of any given size. There are width & height limits. The program should process only one input case (legal or not).
This is my code so far:
Code:
Code:
_________________________________________________
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
char character;
int width =79,
height = 20;
cout << "enter the character, width and height (eg. * 6 3:)";
cin >> character >> width >> height;
for (int i = 1; i <= height; i++) {
for (int j = 1; j <= width; j++)
if ( i == 1 || i == height)
cout<< character;
else
cout<<"ERROR: The height must be between 1 and 20 inclusive!";
cout<<"\n";
if (j == 1 || j == width )
cout<< character;
else
cout<<"ERROR: The width must be between 1 and 79!";
cout<<"\n";
}
return 0;
}
_________________________________________________
I can get the box to print.
However when I try to set height and width limits, the program does not do what it is suppose to do.
Also, how would you print the box with a hollow center.
Any help would be great!
Thanx!
CODE TAGS added by Hammer
-
I answered a question like this a while ago...search the C and C++ boards for your answer.
//edit: Hmmm...that's weird...I can't seem to find it :confused:
-
Anyway...for a hollow box, try...
#include <iomanip>
...
cout<<"|"<<setw(width-2)<<"|"<<endl;
setw() sets a certain amount of blank spaces.
Anyway, try this instead:
Code:
/*code with filled spaces*/
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
char character;
int width =79,
height = 20;
cout << "enter the character, width and height (eg. * 6 3";
cin >> character >> width >> height;
if(width > 79 || height > 20)
{
cout<<"ERROR: Not a valid width/height pair!"<<endl;
return 0;
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j <width; j++)
cout<< character;
cout<<endl;
}
return 0;
}
Code:
/*using setw()*/
#include <iostream>
#include <iomanip>
using std::cout;
using std::cin;
using std::endl;
using std::setw;
int main()
{
char character;
int width,
height;
cout << "enter the character, width and height (eg. * 6 3";
cin >> character >> width >> height;
if(width > 79 || height > 20)
{
cout<<"ERROR: Not a valid width/height pair!"<<endl;
return 0;
}
for(int x=0; x<width; x++)
cout<<character;
cout<<endl;
for (int i = 0; i < height-2; i++)
cout<<character<<setw(width-1)<<character<<endl;
for(int j=0; j<width; j++)
cout<<character;
cout<<endl;
return 0;
}
-
box drawing
I revamped my code, and in all my sample runs it works beautifully except two instances:
when I enter a height of 1, it prints 2 rows
And when I enter a width of one, it prints 2 columns
I'm also trying to understand why you would subtract 2 from the height, and 1 from the width. The best that I can understand it, is that this is some kind of adjustment for the middle rows.
I'm just learning C++, and would like to understand why this would be done!
Thanx for your help!!
following is the new code:
Code:
_____________________________________________
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <iomanip>
using std::setw;
int main()
{
char character;
int width,
height;
cout << "Enter the character, width and height (eg. * 6 3)";
cin >> character >> width >> height;
if(width > 79 && height > 20)
{
cout<<"\nERROR:\tThe width must be between 1 and 79!\n";
cout<<"\tThe height must be between 1 and 20 (inclusive)!"<<endl;
return 0;
}
if(width > 79)
{
cout<<"ERROR: The width must be between 1 and 79!"<<endl;
return 0;
}
if(height > 20)
{
cout<<"ERROR: The height must be between 1 and 20 (inclusive)!"<<endl;
return 0;
}
for(int x=0; x<width; x++)
cout<<character;
cout<<endl;
{
for (int i = 0; i < height-2; i++)
cout<<character<<setw(width-1)<<character<<endl;
for(int j=0; j<width; j++)
cout<<character;
cout<<endl;
}
return 0;
}
__________________________________________
-
for the love of God use code tags: http://cboard.cprogramming.com/showt...threadid=13473
What I gave you was just a hack to show you the idea of setw()
just make it so they can't enter a width or height of 1. I mean...a box has a 2x2 minimum anyhow, doesn't it? think about it...
*
is a single asterisk.
*
*
is a vertical line
**
is a horizontal line
**
**
is the smallest acceptable box.
you subtract 2 from the height because you're performing 2 seperate output operations before and after the main output routine.
You subtract one from the width because you've already outputted one asterisk.