954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Draw a tree homework problem

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;
}
HardToHandle
Newbie Poster
4 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

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

Labdabeta
Posting Pro in Training
489 posts since Feb 2011
Reputation Points: 27
Solved Threads: 18
 

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!

HardToHandle
Newbie Poster
4 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: