adding stars

hi there people, i am new to this forums and i am a beginner at c++.
i have tried reading and using google for the answers that i want, but i dont know what to type in to search for the answers im looking for. so now im here.

my question is how do i make my c++ program to have an output like this:

*
**
***
****
*****

instead of what i have is:
*
*
*
*
*
*
*
*
*

my c++ code so far is:

#include <iostream>
using namespace std;

int main()
{

for(int i = 0; i < 5; i++)

for(int j = 0; j <= i; j++ )
{
cout << "*"<<endl;
}

system("pause");
return 0;
}

sorry if im asking for too much, but it would be very appreciated if anyone can help.

thank you

Huyuga
You're outputting a new line after every star. You should only output the newline at the end of a row.
in this messy program i wrote a while ago i did the same thing, you are welcome to take a look and ask questions.
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <iostream>
#include <cstdlib> 

//data structure for shapes
struct shape{
    int height;
    int length;
    int reverb;
};
// function to produce a triangle
void triangle_1(int height);
// function to produce the 2nd triangle
void triangle_2(int height, int reverb);
// function to produce the 3rd triangle
void triangle_3(int height, int reverb);
//function to produce 4th triangle
void diamond(int height, int length);
// function to produce square
void square_shape(int width);

int main(){
    // defines shapes 
    shape triangle, square, triangle2, triangle3, triangle4;
    // into and prompts user for the length of square 
    std::cout << "*** The Incridible Shape Drawing Program ***\n";
    std::cout << "Let's draw a rectangle please enter a number for the width between 2 and 20: ";
    std::cin >> square.length;
    // length of square size requirements
    for(square.length = square.length; square.length < 2 || square.length > 20; --triangle.reverb) {
        std::cout << "You entered an invalid number...\n Enter a new number: ";
        std::cin >> square.length;
    }
    //prompts user for height of square 
    std::cout << "Please enter a number for height between  1 and 10, (but less than the width: ";
    std::cin >> square.height;
    // height of square size requirements
    for(square.height = square.height; square.length > square.height && (square.height > 10 || square.height < 1); --triangle.reverb){
        std::cout << "You entered an invalid number...\n Enter a new number: ";
        std::cin >> square.height;
    }
    // formating
    std::cout << '\n';
    //using square_shape function and a fore loop to make the square
    for (square.height = square.height; square.height > 0; --square.height){
        square_shape(square.length);
        std::cout << '\n';
    }
    //formating
    std::cout << '\n';
    //prompts user for height of triangle_1
    std::cout << "Lets draw  triangle #1\nPlease enter a number for the height between 2 and 10: ";
    std::cin >> triangle.height;
    // height of triangle_1 requirements
    for(triangle.height = triangle.height; triangle.height < 2 || triangle.height > 10; --triangle.reverb){
        std::cout << "You entered in an invalid number...\n Enter a new number: ";
        std::cin >> triangle.height;
    }
    //formating
    std::cout << '\n';
    //for loop which creates triangle_1
    for(triangle.length = 1; triangle.height > 0; ++triangle.length && --triangle.height){ 
    triangle_1(triangle.length);
        std::cout << '\n';
    }
    //formating
    std::cout << '\n';
    // prompts user for height of triangle2
    std::cout << "Lets draw triangle #2\nPlease enter a number for the height between 2 and 20: ";
    std::cin >> triangle2.height;
    // height of triangle2 requirements
    for(triangle2.height = triangle2.height; triangle2.height < 2 || triangle2.height > 10; --triangle.reverb){
        std::cout << "You entered in an invalid number...\n Enter a new number: ";
        std::cin >> triangle2.height;
    }
    //formating
    std::cout << '\n';
    //for statement to create triangle_2
    for(triangle2.length = 1 && (triangle2.reverb = triangle2.height - 1); triangle2.height > 0; --triangle2.reverb){ 
        triangle_2(triangle2.length,triangle2.reverb);
        std::cout << '\n';
        ++triangle2.length;
        --triangle2.height;
        }
    // prompts user for height of triangle3
    std::cout << "\nLets draw triangle #3\nPlease enter a number for the height between 2 and 20: ";
    std::cin >> triangle3.height;
    // height of triangle3 requirements
    for(triangle3.height = triangle3.height; triangle3.height < 2 || triangle3.height > 10; --triangle.reverb){
        std::cout << "You entered in an invalid number...\n Enter a new number: ";
        std::cin >> triangle3.height;
    }
    //formating
    std::cout << '\n';
    //for statement to create triangle_3
    for(triangle3.length = 1 && (triangle3.reverb = triangle3.height - 1); triangle3.height > 0; --triangle3.reverb){ 
        triangle_3(triangle3.length,triangle3.reverb);
        std::cout << '\n';
        triangle3.length += 2;
        --triangle3.height;
    }
    // prompts user for height of triangle4
    std::cout << "\nLets draw diamond #1\nPlease enter a number for the height between 3 and 10: ";
    std::cin >> triangle4.height;
    // height of triangle4 requirements
    
    for(triangle4.height = triangle4.height; triangle4.height < 3 || triangle4.height > 10; --triangle.reverb){
        std::cout << "You entered in an invalid number...\n Enter a new number: ";
        std::cin >> triangle4.height;
    }
    
    //formating and determining the actual value of variable triangle4.height
       triangle4.height -= 2;
    // this starts the reversing proccess for the value of triangle4.height
    std::cout << '\n';
    
    //for statement to create triangle_3 which is the top half of the diamond
    for(triangle4.length = 1 && (triangle4.reverb = triangle4.height - 1); triangle4.height > 0; --triangle4.reverb){ 
        triangle_3(triangle4.length,triangle4.reverb);
        std::cout << '\n';
        triangle4.length += 2;
        --triangle4.height;
    }
    
    //reseting values in preperation for 2nd half of rhombus 
    triangle4.length -= 4; 
    triangle4.reverb = 1;
    triangle4.height = 1; 
    //creates 2nd half of rhombus
    
    for(triangle4.length = triangle4.length; 0 < triangle4.length; ++triangle4.reverb){ 
        diamond(triangle4.length, triangle4.reverb);
        std::cout << '\n';
        triangle4.length -= 2;
        ++triangle4.height;
    }
        return 0;
}

void triangle_1(int height){
    //for loop to make the width of triangle_1
    for(height = height; height != 0; --height){
        std::cout << "*";
    }
}    

void triangle_2(int height, int reverb){
    //for loop to make the width of triangle_2 empty space
    for(reverb = reverb; reverb != 0; --reverb){
        std::cout << " "; 
    }
    //for loop to make the width of triangle_2 
    for(height = height; height != 0; --height){
        std::cout << "*";
        }
}

void triangle_3(int height, int reverb){
    //for loop to make the width of triangle_3 empty space
    for(reverb = reverb; reverb != 0; --reverb){
        std::cout << " "; 
    }
    //for loop to make the width of triangle_2 
    for(height = height; height != 0; --height){
        std::cout << "*";
    }
}

void diamond(int height, int length){
    //for loop to make the width of diamond empty space
    for(length = length; length != 0; --length){
        std::cout << " "; 
    }
    //for loop to make the width of diamond  
    for(height = height; height != 0; --height){
        std::cout << "*";
    }
}

void square_shape(int width){
    // for loop to make the width of the square
    for(width = width; width != 0; --width){
        std::cout << "*";
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

void Write(int base, ostream& out)
{
	for (int i = 0; i < base; i++)
	{
		out << endl;
		for (int j = 0; j < i+1; j++)
		out << "*";
	}
}

int main() 
{
	
	Write(20, cout);	

	return 0;

}
Last edited on
hi guys,

Thanks for helping me out. so the output that i wanted to display needed functions. Functions looks hard and complicated to understand. i know i'm still new to c++, but i will try my best to learn and understand how functions work.

thank you guys.

@ui uiho, thank you for showing me your code. when i ran your code, it was very neat what you did. although i don't fully understand it yet, i will try my best to understand it.

@IceThatJaw, thank you for re writing the code to the output i wanted to display.

thanks guys for you help

Huyuga
No, it doesn't need functions. Just don't put an endl after each star. It makes a new line after each star.

#include <iostream>
using namespace std;

int main()
{
	for(int i = 0; i < 5; i++)
	{
		for(int j = 0; j <= i; j++ )
		{
			cout << "*";		
		}
		cout << endl;
	}
	
	system("pause");
	return 0;
}
@frema, thank you for showing that to me. Now i understand a bit more.

Thank you

Huyuga
glad it helped. remember no system functions.
hi again guys,

@ui uiho, I,m reading your code and its pretty confusing for a beginner like me, and i want to do something similar to yours where you ask to do a diamond. Your output had whitespace's in front of the stars. I want to do something similar to that, with the output looking something like this:

*+
**++
***+++
****++++

I cant seem to make it work. I tried doing cout<< "*"<<"+";
but that doesn't seem to work. So i am kinda mind boggled right now.

I hope you guys help

Thanks

Huyuga

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int main()
{
	for(int i = 0; i < 5; i++) // repeats 5 times
	{
		for(int j = 0; j <= i; j++ ) //repeats the number of times i is
		{
			cout << "*";		
		}
		for(int j = 0; j <= i; j++ )//repeats the number of times i is
		{
			cout << "+";		
		}

		cout << endl; //adds a new line 
	}
	
	system("pause");
	return 0;
}
@oonej, thanks for your reply and the code you provided. Thank you

Now what if i wanted to make an output like this:


*+
**++
***+++
****++++
*****+++++
*****-----
****----
***---
**--
*-


the code that i put in comes out like this:


*+
**++
***+++
****++++
*****+++++
*****-----
*****-----
*****-----
*****-----
*****-----


so how would i make the characters disappear 1 by 1 on each line?

this is the code i used with oonej's code with a bit extra:

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
#include <iostream>
using namespace std;

int main()
{
	for(int i = 0; i < 10; i++) 
	{
		for(int j = 0; j <= i && j <= 5 && i <= 5; j++ )
		{
			cout << "*";		
		}
		for(int j = 0; j <= i && j <= 5 && i <= 5; j++ )
		{
			cout << "+";		
		}
                        for (int j = 5; j <= i && j>= 0 && i > 5 && i <=10; j--)
                               {
                                       cout<< "-";
                                }
                        for (int j = 5; j <= i && j>= 0 && i > 5 && i <=10; j--)
                               {
                                        cout<< "+";
                               }
		cout << endl; 
	}
	
	system("pause");
	return 0;
}


i hope there is way you guys can help me.

Thank you

Huyuga
Last edited on
i would suggest functions which would make your code simpler. otherwise split the triangle construction into 2 separate nested for loops, one that makes the top half, and the second makes the bottom half. because at this time you are not making the bottom half, just continuing the last line 5 more times.
@ui uiho, yes i had a look at funtions and it does look neater. However, it is a bit complicated at the moment for me, as it will take time for me to understand. Thank you for your tip as it was helpful. I really did appreciate you helping me. So thank you ui uiho

as for what i was looking for, i have finally cracked what i wanted to do so here is the code that i have done

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
#include <iostream>
using namespace std;

int main()
{
	for(int i = 0; i < 10; i++)
	{
		for(int j = 0; j <= i && i <= 4; j++ ) 
		{
			cout << "*";		
		}
		for(int j = 0; j <= i && i <= 4; j++ )
		{
			cout << "+";		
		}
                        for (int j = i; j >= 5 && j < 10; j++)
                                   {
                                     cout<< "-";
                                    }
                        for (int j = i; j >= 5 && j < 10; j++)
                                    {
                                     cout<< "+";
                                    }
		cout << endl; 
	}
	
	system("pause");
	return 0;
}


Once again, thank you guys for helping me solve this case

Cheers

Huyuga
Last edited on
Topic archived. No new replies allowed.