Having trouble designing a box

I am supposed to create a box based on user input. For example:

Enter the number of rows (max 99): 10
Enter the number of cols (max 99): 20
Enter an interior character: |
Enter an exterior character: +
+ + + + + + + + + + + + + + +
+ | | | | | | | | | | | | | | | | | | +
+ | | | | | | | | | | | | | | | | | | +
+ | | | | | | | | | | | | | | | | | | +
+ | | | | | | | | | | | | | | | | | | +
+ | | | | | | | | | | | | | | | | | | +
+ | | | | | | | | | | | | | | | | | | +
+ | | | | | | | | | | | | | | | | | | +
+ | | | | | | | | | | | | | | | | | | +
+ + + + + + + + + + + + + + +

Could anyone help me get started? I am having a hard time with this for a C++ class! Thanks :)
Last edited on
roughly... but what if they enter 1 and 1 ? needs a tweak for that but this should get you started

for(i = ... all the rows)
{
cout <<exterior;
for (j = ...all the cols-2)
cout << interior;
cout << exterior << endl;
}

I would be happy to help you. I would recommend using nested loops to solve the problem. In any problem where you need to output characters in rows and columns nested loops can help you get a resolution. Simply create a loop on the outside that represents the rows and loop on the inside that represents the column. The loop that represents the column should print a character to the console. The loop that represents the row should move to the next line. The catch in this problem is that we need to print a different character depending on the position. I would recommend writing a conditional statement inside the column loop that determines which character to print.

How do we determine which coordinates are the exterior points? If we consider a graph with coordinates x and y or better yet an array with two dimension we can begin to visualize our solution. In the array box[x][y] we can easily determine the exterior coordinates or values. So how do we define the boundaries of our array? We know one set of boundaries just be examining the properties of a two-dimensional array. We know that 0th element of the x coordinate is always a boundary and the 0th element of the y coordinate is always a boundary. The outer boundary will be determined by the user input.

y = 0
+ + + + + + + + + + + + + + + x = userInput
+ | | | | | | | | | | | | | | | | | | +
+ | | | | | | | | | | | | | | | | | | +
+ | | | | | | | | | | | | | | | | | | +
+ | | | | | | | | | | | | | | | | | | +
+ | | | | | | | | | | | | | | | | | | +
+ | | | | | | | | | | | | | | | | | | +
+ | | | | | | | | | | | | | | | | | | +
+ | | | | | | | | | | | | | | | | | | +
+ + + + + + + + + + + + + + + y = userInput
x = 0

Simply put
every coordinate (x,y) is exterior if x is 0 or x is userInput
every coordinate (x,y) is exterior if y is 0 or y is userInput

You should be able to use a conditional statement to reflect this logic and then output the proper character
Last edited on
I'm working on the same problem and so far I have the following. I can't figure out what I'm missing.

#include <iostream>
#include <string>
using namespace std;

int main () {
int rows, columns, x, y;
string intCharacter, exCharacter;
x = 0;
y = 0;
cout << "Enter the number of rows (max 99): ";
cin >> rows;
cout << rows << endl;
cout << "Enter the number of cols (max 99): ";
cin >> columns;
cout << columns << endl;
cout << "Enter an interior character: ";
cin >> intCharacter;
cout << intCharacter << endl;
cout << "Enter an exterior character: ";
cin >> exCharacter;
cout << exCharacter << endl;
while (x < rows) {
y = 0;
while (y < columns) {
y = y + 1;
if (x == 0) {
cout << exCharacter;
}
if (x == rows -1) {
cout << exCharacter;
}
if (y == 1) {
cout << exCharacter;
}
if (y == columns - 1) {
cout << exCharacter;
}
}
x = x + 1;
cout << endl;
}
return 0;
}

I am also having a hard time with this same problem and cannot figure out what I have done wrong. Can anyone help or did any of you get this to run properly??

#include <iostream>
#include <string>
using namespace std;

int main () {
int rows, cols, x, y;
string intChar, extChar;


cout << "Enter the number of rows (max 99): ";
cin >> rows;
cout << rows << endl;

cout << "Enter the number of cols (max 99): ";
cin >> cols;
cout << cols << endl;

cout << "Enter an interior character: ";
cin >> intChar;
cout << intChar << endl;

cout << "Enter an exterior character: ";
cin >> extChar;
cout << extChar << endl;

while (x < rows) {


while (y < cols) {

if(y == 0 || y == rows - 1){
cout << extChar << " ";
}

else if(x == 0 || x == cols - 1){
cout << extChar << " ";
}
else
cout << intChar << " ";
}
cout << endl;

}

return 0;
}
Topic archived. No new replies allowed.