Asterisks Shapes Together

I need help in writing a project in which dimensions are read in for different shapes and I need to output these shapes next to each other with same boundaries based on user input. I have no difficulty in making the shapes, but in printing them out next to each other horizontally.

Here is my code:

#include<iostream>
using namespace std;

int main()
{
//Declare variables
int numStars, length, width, numRuns, numTimes;
char shapeType;

cin>>numRuns;
cout<< endl;

//Loop for all runs
for(int b=1; b<=numRuns; b++)
{
cout<<"Please enter box type and dimensions: ";
cin>> shapeType;

//Rectangle shape

if (shapeType=='B' or shapeType=='b')

{
cin>>width>>length>>numTimes;

for (int i=0; i<length; i++)
{
for (int a=0; a<width*numTimes-1; a++)
{
cout<<"* ";

}
cout <<endl;
}

}

//Triangle shape
if (shapeType=='L' or shapeType=='l')

{
cin>> numStars>>numTimes;

for(b=0; b<=numTimes; b++)
{
for (int a=1; a<=numStars; a++)
{

for(int j=1; j<=a; j++)
{
cout<<"* ";

}

cout<<endl;

}
}

}




//Square shape
if(shapeType== 'S' or shapeType== 's')
{
cin>>numStars;

for (int width=0; width< numStars; width++)
{
for (int length=0; length< numStars; length++)
{
cout << "* ";
}
cout << endl;
}

}
//Reversed triangle shape
if (shapeType== 'H' or shapeType=='h')
{
cin>>numStars;

for(int i=1;i<=numStars; i++)
{
for(int j=1; j<=numStars; j++)
{
if(j>=i)
{
cout<<"*";
}
else
{
cout<< " ";
}
}
cout<< endl;
}
}

}
return 0;
}
Last edited on
Please put your code sample in code tags and provide input/output samples to indicate what you mean. (You can use the format menu to maintain fixed spacing.)

If you are putting several shapes adjacent to each other horizontally you may find it easier to create them on a “canvas” made up of an array of strings, one for each line. At the end you print out the canvas.
Is there still a way to do this using for loops or if statements?
lastchance's suggested way would still be using for loops and if statements when you are actually doing the logic for each individual shape.
Last edited on
@OP Here's a start - note the extra j-loop for a box. You also need some prompts instead of a blinking cursor so the end-user to knows what is going on.

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include<iostream>
using namespace std;

int main()
{
    //Declare variables
    int numStars, length, width, numRuns, numTimes;
    char shapeType;
    
    cin>>numRuns;
    cout<< endl;
    
    //Loop for all runs
    for(int b=1; b<=numRuns; b++)
    {
        cout<<"Please enter box type and dimensions: ";
        cin>> shapeType;
        
        //Rectangle shape
        
        if (shapeType=='B' or shapeType=='b')
            
        {
            cin>>width>>length>>numTimes;
            
            for (int i=0; i<length; i++)
            {
                for(int j = 0; j < numTimes; j++)
                {
                    
                    for (int a=0; a<width; a++)
                    {
                        cout<<"* ";
                        
                    }
                    cout << '-'; // choose a spacer character or string to suit
                }
                cout <<endl;
            }
            
        }
        
        //Triangle shape
        if (shapeType=='L' or shapeType=='l')
            
        {
            cin>> numStars>>numTimes;
            
            for(b=0; b<=numTimes; b++)
            {
                for (int a=1; a<=numStars; a++)
                {
                    
                    for(int j=1; j<=a; j++)
                    {
                        cout<<"* ";
                        
                    }
                    
                    cout<<endl;
                    
                }
            }
            
        }
        
        
        
        
        //Square shape
        if(shapeType== 'S' or shapeType== 's')
        {
            cin>>numStars;
            
            for (int width=0; width< numStars; width++)
            {
                for (int length=0; length< numStars; length++)
                {
                    cout << "* ";
                }
                cout << endl;
            }
            
        }
        //Reversed triangle shape
        if (shapeType== 'H' or shapeType=='h')
        {
            cin>>numStars;
            
            for(int i=1;i<=numStars; i++)
            {
                for(int j=1; j<=numStars; j++)
                {
                    if(j>=i)
                    {
                        cout<<"*";
                    }
                    else
                    {
                        cout<< " ";
                    }
                }
                cout<< endl;
            }
        }
        
    }
    return 0;
}
Topic archived. No new replies allowed.