Help with Tab Command

Greetings! This is my first post in this forum. I hope everyone is doing well. I'm new to C++ and when I say new, I mean it's only been a month that I've even seen anything resembling code. I'm taking a c++ class and I want to be upfront, i'm NOT looking for answers, I just need help. My work schedule does not allow me to attend tutoring hours. So thank you in advance.
My assignment is to create a filled box and a hollow box using nested statements. I've completed that portion, but I'm confused as to how to get my boxes side by side? Any assistance would be greatly appreciated. This is my code:
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
32
33
34
35
36
#include <iostream>
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    cout << "This program prints a filled and hollow square.\n\n";
    cout << "Enter the length of a side: ";
    int size;
    cin >> size;

        for (int row = 1; row <= size; row++) {
            for (int col = 1; col <= size; col++) {
                cout << "*";
            }

     cout << endl;

  }


        for (int row = 1; row <= size; row++){
            for (int col = 1; col <= size; col++) {
                if (row > 1 && row < size && col > 1 && col < size)
                    cout << " ";
                    else
                        cout << "*";
    }


     cout << endl;
  }

    return 0;
}
Last edited on
Hi there, that's an interesting assignment right there. I am also new to C++, I've been teaching it to myself for the past month or so with online tutorials. But anyways I believe I had a bit more luck solving your problem.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    cout << "This program prints a filled and hollow square.\n\n";
    cout << "Enter the length of a side: ";
    int size;
    cin >> size;
         for (int row = 1; row <= size; row++) {
            for (int col = 1; col <= (size*2) + 1; col++) {
                if (col == size + 1)
                    cout << " ";
                if ((col > size+1 && col < (size*2) +1) && (row > 1 && row < size))
                    cout << " ";
                else
                    cout << "*";

            }
     cout << endl;
  }
return 0;
}

I hope you understand what I did and how it all works, If not just holler and I'll gladly help you.
BTW you can't create 2 boxes of sizes greater than 39 because it will lose it's format due to the limited console window
P.S. I also added an empty column in between boxes for good measure ;)
Last edited on
Wow! Thank you so much! I have been puzzling over this for hours. I'm really trying to understand this stuff. If you have time, could you explain how you arrived at (size*2) + 1?
I used that formula twice in the program, so I'm not sure witch one are you referring to.
1st:
1
2
3
for (int col = 1; col <= (size*2) + 1; col++) {
                if (col == size + 1)
                    cout << " ";

Notice what I did whit bolded text. It was to add a line in between the boxes so if not for that +1 the alignment would be off.

2nd:
Well, that line of code isn't really perfect so this:
(col > size+1 && col < (size*2) +1)
Can also be written as:
(col > size+1 && col <= (size*2))
which is preferable because it's easier to understand, again, because of that extra line in between the boxes it is there to fix the formating.

In any case you should have tried just changing those statements as you wish and compile the progrem to see what has changed. You probably could figure it out by yourself that way.
Topic archived. No new replies allowed.