0

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;
}

Edited by HardToHandle: n/a

2
Contributors
2
Replies
5
Views
5 Years
Discussion Span
Last Post by HardToHandle
1

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

0

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!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.