Thread: Box Drawing Program

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    5

    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

  2. #2
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    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
    Last edited by -KEN-; 02-09-2003 at 12:05 PM.

  3. #3
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    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;
    }
    Last edited by -KEN-; 02-09-2003 at 12:24 PM.

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    5

    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;


    }

    __________________________________________

  5. #5
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No data showing in combo box
    By PJYelton in forum Windows Programming
    Replies: 6
    Last Post: 04-29-2005, 07:20 PM
  2. How to program a "back" button with MFC
    By 99atlantic in forum Windows Programming
    Replies: 3
    Last Post: 04-26-2005, 08:34 PM
  3. Drawing program architecture - information needed
    By Marko_D in forum Windows Programming
    Replies: 1
    Last Post: 11-28-2003, 07:46 PM
  4. drawing a box
    By allthekandi in forum C++ Programming
    Replies: 4
    Last Post: 03-08-2003, 07:39 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM