Christmass tree c++ help me.

I need help with an exercise that was given to me ages ago.. I just remembered it and realized that i never really got the answer..

This exercise is making a Christmas tree..

______* //the underlines are blank space
_____***
____***** <--(this i can do)
___*******
__*********
like so.. but instead of that i need spaces between * (stars)..
like this:

______*
_____*_*
____*_*_*
___*_*_*_*

i know how to do the math but can't really put it in the codes.. I'm stuck.

this is my code without the space between stars.
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
  #include <iostream>

using namespace std;

int main() {
	
	int height, stars = 1;
	
	cout << "This program will create a christmass tree." << endl;
	cout << "Just enter the height you want the tree to be." << endl;
	cout << endl << "Enter the height of the tree: ";
	cin >> height;
	cout << endl << endl;

	for(int i = 0; i < height; height--) {
		for(int p = 0; p < height; p++)
			cout << " ";
		for(int y = 0; y < stars; y++)// should i have a for-loop
			cout << "*"; // inside this for-loop?
		stars += 2;
		cout << endl;
	}
	system("pause>0");
	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
35
36
37
38
39
40
41
#include <iostream>
#include <iomanip>
using namespace std;


int main()
{
	cout << "Enter the height of the tree: " << flush;
	int Height = 0;
	while (!(cin >> Height))
	{
		cin.clear();
		cin.ignore();
		cout << "Enter the height of the tree: " << flush;
	}
	cin.ignore();

	int spaceCount = 0;
	int indent = Height;
	for (int i = 1; i <= Height; i ++)
	{
		int count = i + spaceCount;
		cout << setw(indent) << ' ';
		indent--;
		while (count > 0)
		{
			if (count % 2 == 0)
				cout << " ";
			else
				cout << "*";

			count--;
		}
		spaceCount++;
		cout << endl;
	}


	cin.ignore();
	return 0;
}
Yeah i found the answer using my codes..
1
2
3
4
5
6
7
8
9
10
for(int i = 0; i < height; height--) {
		for(int p = 0; p < height; p++)
			cout << " "; 
		cout << "*";
		for(int t = 1; t < stars; t++)
			cout << " " << "*";
		
		stars++;
		cout << endl;
	}

but i did learn something about failed input from you..
Topic archived. No new replies allowed.