i am having problems with this assignment :

Write a program that prints an N by N box on the screen as follows:

Please enter a number between 2 and 10: 5
-----
|   |
|   |
|   |
-----
Press any key to continue . . .

Note that there are 5 dashes (-) on the top and bottom row, and pipe characters (|) 
for the sides in the middle rows. In the example there are 3 spaces between each 
pipe character, making 5 characters across.  So, this is a 5 by 5 box.

Check that the number is between 2 and 10; if out of bounds, display an error message 
and END THE PROGRAM (do not keep asking for a valid number). 

For example:

Please enter a number between 2 and 10: 11
That number is out of range.
Press any key to continue . . .

ANOTHER EXAMPLE:
Please enter a number between 2 and 10: 9
---------
|       |
|       |
|       |
|       |
|       |
|       |
|       |
---------
Press any key to continue . . .

so far i have:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int numExs, sum = 0;

    cout << "Please enter a number between 2 and 10: " ;
    cin >> numExs;

    if ((numExs < 2) || (numExs > 10))
    {
        cout << "Entry is out of range";
    }
    else
    {
        for (int row = 1; row <= numExs; row++)
        {
            cout << "|" ;
            for (int x = 1; x <= numExs; x++)
            {
                cout << "-" ;
            }

            cout << "|" << endl;
        }
    }
        cout << endl;
}

can anyone help me out??
thanks.

Recommended Answers

All 6 Replies

I am working on this one. Here is what I have so far. I got the top and bottom, I just need to figure out how to get five down the side..lol. Well, here is what I have so far if this will help:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int numChars;
    int total = 1;

    cout << "Please enter a number between 2 and 10: ";
    cin >> numChars;

    if ((numChars >= 2) && (numChars <= 10))
    {
        for (int row1 = 1; row1 <= numChars; row1++)
        {
            cout << "-";
        }
        cout << endl;
        for (int row2 = 0; row2 <= total; row2++)
        {
            cout << "|";
        }
        cout << endl;
        for (int row3 = 1; row3 <=numChars; row3++)
        {
            cout << "-";
        }
        cout << endl;

    }
    else
    {
        cout << "That number is out of range. " << endl;
    }
}

For stuff like this you really need code tags:

Without code tags:

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

With code tags:

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

Simply draw your box, then put code tags around it:

[code]

put box here

[/code]

Lots of similar threads on Daniweb. Here are two:

http://www.daniweb.com/forums/thread109218.html
http://www.daniweb.com/forums/thread111291.html

Here's your code formatted and with code tags:

#include <iostream>
#include <string>

using namespace std;

int main()
{
	int numExs, sum = 0;

	cout << "Please enter a number between 2 and 10: " ;
	cin >> numExs;

	if ((numExs < 2) || (numExs > 10))
	{
		cout << "Entry is out of range";
	}
	else
	{
		for (int row = 1; row <= numExs; row++)
		{
			cout << "|" ;
			for (int x = 1; x <= numExs; x++)
			{
				cout << "-" ;
			}

			cout << "|" << endl;
		}
	}
	cout << endl;
}

What in particular are you having problems with?

Basically we got the top part to work, mean the ------ parts, we are just having a little trouble with the middle part of the box...the | parts. I hope I explained that in a way that it made sense.

If it helps, here is the code that I have. I know it doesn't have any coding, but it's 4:15 am here and I'm tired....lol. But here is my code. It works for the first number, but now do I get it to work for the second number.

here is the code:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int numChars;
    int total = 1;

    cout << "Please enter a number between 2 and 10: ";
    cin >> numChars;

    if ((numChars >= 2) && (numChars <= 10))
    {
        for (int row1 = 1; row1 <= numChars; row1++)
        {
            cout << "-";
        }
        cout << endl;
        for (int row2 = 0; row2 <= total; row2++)
        {
            cout << "|" << '\t';
        }
        cout << endl;
        for (int x = 0; x <= total; x++)
        {
            cout << "|" << '\t';
        }
        cout << endl;
        cout << "|" << '\t' << "|" << endl;
        for (int row3 = 1; row3 <=numChars; row3++)
        {
            cout << "-";
        }
        cout << endl;

    }
    else
    {
        cout << "That number is out of range. " << endl;
    }
    return 0;
}

i almost have it figured out, but can't figure out what is wrong with the middle part. what else is missing or wrong?

Basically we got the top part to work, mean the ------ parts, we are just having a little trouble with the middle part of the box...the | parts. I hope I explained that in a way that it made sense.

Well it's a three stage process. First stage is to draw the top. Middle stage is to draw the sides. Last stage is draw the bottom, which is identical to the first stage. Sounds like you have to the first and last done. You'll do best to break these into these three stages and do them in order. Don't try to mingle them.

Middle stage:

You are going to do the same thing repeatedly: draw the left side, draw some spaces, then draw the right side. This is one line. You're going to do this line over and over again, the exact same thing, so it calls for a loop. Go through that loop once per line.

As for what goes inside that loop, you're going to draw the left side character, then draw a bunch of spaces, then the right side character. So you are undoubtedly going to end up with a loop inside of another loop. Check out those links. It's a common problem.

for (int i = 0; i < ?; i++)
{
     // display something
     for (int j = 0; j < ?; j++)
     {
          // display something
     }
     // display something
     // end of line
}

thanks. i'll try it. I'm gonna take a break and get up early in the morning and finish. thanks for your help. i appreciate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.