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:
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.
#include <iostream>
#include <iostream>
#include <iomanip>
usingnamespace 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 ;)
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.