Hello guys,

So my task is the following:

Draw a pine tree based on user inputted height.

How tall should the tree be?: 6
/\
/ \
/ \
/ \
/ \
/ \
------------
||
||
||

I had problems moving the cursor from the left to right in order to draw the backlash characters, however, I managed to do some of it, but they do not align in the way they should. I would appreciate any help, here is my code:

#include <iostream>
#include <stdlib.h>
using namespace std;

int main ()
{
	int height;

	cout << "Please enter a height for the tree: " << endl;
	cin >> height;


	for (int level = 0; level < height; level++)
	{
	   for (int spaces = 0; spaces < height - level - 1; spaces++)
		   cout << ' ';
	       
	    for (int a = 0; a < 2 - 1; a++)
           cout << '/' << endl;

		for (int middle = 0; middle < 2 * level + height; middle++)
			cout << ' ';
		  for (int b = 0; b < 2 - 1; b++)
			  cout << '\\'<< endl;
		 
		 
		       
	}
	

	return 0;
}

Recommended Answers

All 2 Replies

The problem is on line 19 when you output an endl (newline) after the forward slash. Remove the endl and it should work fine.

Thanks a lot, I did not realize that was the problem. I also removed height from line 21, and now the two sides of the tree match evenly. Thanks a lot for the help, now all I have to do is figure out how to draw the trunk using a for loop. Cheers!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.