Trying to make a hollow square

Okay so I'm trying to make a hollow square out of whatever characters are being inputted by the user. I got the solid square part down. But I've tried basically everything that I'm aware of to try and get rid of everything in the middle.

I'm using a range based for loop to start it off since it'll automatically detect how many characters you input.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 int main(){
    
    string characters {};

    cout << "Enter in some characters and it'll print in a square : ";
    getline(cin, characters);
    
    for (auto c: characters){
        for (int r {0}; r < characters.size(); --r){
            if (characters.max_size())
            cout << endl;
        }
        if (characters.at(0) || characters.max_size())
            cout << characters;
    }
    
    cout << endl;
    return 0;
}
Thank you for formatting your code. When you post an example, please also paste the stuff above main, otherwise we have to manually add it ourselves to get your code to compile.

if (characters.max_size())

if (characters.at(0) || characters.max_size())
What are you expecting this to be checking for? characters.at(0) simply returns the first character of your string. characters.max_size() is some super huge number that you shouldn't need to worry about. Both of those are then evaluated in a boolean statement. i.e. it doesn't make much sense.

Any non-zero expression in an if-statement always evaluates to true.

What exactly do you want the output to be?
e.g. if I input "12345", what should the output be?
Currently it's
12345
12345
12345
12345
12345


Did you want it to be
12345
1   5
1   5
1   5
12345
?

If so, look at the pattern: The first line is always the full string. The last line is always the full string. The middle part happens ([num rows] - 2) times, and the first characters, then [num rows - 2] spaces, then the last character. Think about that pattern as you formulate your if statements.

Or, did you want something more creative, like:
12345
2   4
3   3
4   2
54321

If it's the latter, then there's still a pattern to code to, it's just a bit trickier. The first string is normal. The last string is reversed. For the outer characters in the middle strings, notice that the index for the left character goes up by 1 each time, and the index for the right character does down by 1 each time.

It will probably be easier for you to just use a normal indexed for loop for this. Use the current index to help decide which character index to print.
Last edited on
Right, from now on I'll post the entire code, thanks.

When you put it that way, I'm not really sure why I even put those in the if statement since they aren't doing anything. I guess I was just spitballing there.

But yes, that is what I want the output to be.
Okay [for those reading, Rogromi meant my first output, it was while I was editing my post],
so then your pseudo-code will be something like:

1
2
3
4
5
6
7
8
9
print string as normal w/ newline

repeat (length of string - 2) times:
    print first character
    print (length of string - 2) spaces.
    print last character
    print new line

print string as normal w/ newline
Last edited on
#include <iostream>
#include <string>
using namespace std;

int main(){

string characters {};
string spaces {" "};
size_t num_spaces {};
cout << "Enter in some characters and it'll print in a square : ";
getline(cin, characters);


cout << endl;
cout << characters << endl;

num_spaces = characters.size() - 2;

for (int c {0}; c < num_spaces; ++c){
cout << characters.front();
for (int r {0}; r < num_spaces; ++r) {
if (num_spaces == characters.size() - 2)
cout << spaces;
}
cout << characters.back();
cout << endl;
}

cout << characters << endl;

cout << endl;
return 0;
}

Mission accomplished, thanks for the help, Ganado :)

Edit : Just deleted the if statement in the second loop and still works as expected.
Last edited on
Topic archived. No new replies allowed.