need help creating hollow triangle using for loops

for a school project i have to try to make a hollow triangle whose size is based on the user's input. so far i have a good portion of it down but for some reason it will not print any asterisks. without giving me the answer, could someone help me with it? here is my code so far:

#include <iostream>

using namespace std;

int main(void)
{
int size,des_s,i,triangle,k;
(i=0);
(triangle=0);
(k=0);


cout<<"please enter the size of the triangle ";
cin>>size;
(des_s=size);
for (int triangle=0 ; triangle>3 ;triangle++)
{
if (i!=0 and i!=size) //this was supposed to help with not only making * at the edge of the triangle but to also determine how many spaces there are inbetween the two *
{
for (i=1 ; i<des_s ; i++, des_s--)
{
cout <<" * ";
for (int j=1 ; j<size ; j++)
{
cout<<" ";
}
cout<<" * "<<endl;
}
}
if (i==0) //this should test to see if it is the first line of the triangle
{
cout<<" * "<<endl;
(i++);
}
else
{
for (k=1 ; k=size ; k++)// this should test to see if it is the last line and execute accordingly
{
cout<<" * ";
}
}
}
return 0;
}

Last edited on
Use code tags when posting code to preserve indentation.
1
2
3
(i=0);
(triangle=0);
(k=0);

You don't need to parentheses around these assignments.
for (int triangle = 0; triangle > 3; triangle++) {
This loop will never execute. In English, you're telling the computer:
"Set triangle to zero. Now if triangle is greater than three, execute this loop". Since the initial value of triangle is zero, and zero is NOT greater than 3, the loop never executes.

for (k=1 ; k=size ; k++) k=size is wrong for 2 reasons. First, = is the assignment operator. == is the comparison operator. But in this case, I think you want k!=size. Otherwise the loop will execute forever.

Is this what it's supposed to look like?
$ ./foo
please enter the size of the triangle 6
 *
 * *
 *   *
 *     *
 *       *
 **********

$ ./foo
please enter the size of the triangle 4
 *
 * *
 *   *
 ******


To write code for this, I put a comment in the code with an example of the output. I labelled the lines with the number of spaces. This helped determine the loops:
1
2
3
4
5
6
7
// Example for size = 5
//  012345679
// 0 *                  1 star
// 1 * *                1 space
// 2 *   *              3 spaces
// 3 *     *            5 spaces
// 4 ********           8 stars 

thanks! my teacher wants us to make something along the lines of

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//size=3 (user imputed)

//     *    
//   *   *
// *   *   * 

// if the size is something like 5 then

//       *     
//     *   *
//    *      *
//  *          *
//  ********
// sorry for the bad picture 

i did learn a little bit more about the for loops and how they are structured, many where it comes to how it determines how many times to loop but i still struggle when making shapes
Last edited on
Using your editor and a fixed-width font, create a file containing a couple of the triangles exactly as they should appear.

Label the rows and columns the way I did.

Count the various spaces and add them to the end of the file as I did.

Figure out a mathematical expression that gives the right number of spaces.

Now write the code.

P.S. If you have learned about functions, you might want to create a small function that writes a given character, a given number of times:
1
2
3
4
void repeat(char ch, int n)
{
    // print "n" copies of "ch" to cout
}


Now you can do things like this in your main progarm:
1
2
repeat(' ', 3); // print 3 spaces
repeat('*', 2*i+1);  // print 2i+1 stars 


Topic archived. No new replies allowed.