Nice

Edit your post and add [code] and [/code] around your code and output to format it.

https://i.kym-cdn.com/photos/images/original/000/450/154/820.jpeg
Last edited on
Your code runs OK on my machine. Possibly the problem you're having is because the rectangle is scrolling on a 80 column monitor.

The rectangle lends itself to the efficiencies of using functions. Here is one way, there are plenty of opportunities to improve on that too.

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
#include <iostream>

void line( int size, char border, char middle)
{
    std::cout << border;
    
    for(int i = 0; i < size - 2; i++)
        std::cout << middle;
    std::cout << border << '\n';
}

int main(void)
{
    
    int size = 0;
    std::cout << "Enter the size of the square: ";
    std::cin >> size;
    
    line(size, '+', '-');
    
    for (int i = 0; i < size - 2; i++)
        line(size, '|', ' ');
    
    line(size, '+', '-');
    
    return 0;
}

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
#include <iostream>

void line( int size, char border, char middle)
{
    std::cout << border;
    
    for(int i = 0; i < size - 2; i++)
        std::cout << middle;
    std::cout << border << '\n';
}

int main(void)
{
    
    int size = 0;
    while
        (
         std::cout << "Enter the size of the square: " and
         std::cin >> size and
         size > 100
         )
    {
        std::cout << "Too big\n";
    }
    
    line(size, '+', '-');
    
    for (int i = 0; i < size - 2; i++)
        line(size, '|', ' ');
    
    line(size, '+', '-');
    
    return 0;
}


I forgot to mention, sorry. As you can see, sorry is not necessary. You should have initialized it.
Last edited on
I'm glad he/she got the answer they wanted.

It's over to the teacher to see how many turn it in. I might be the teacher ... :)
Topic archived. No new replies allowed.