Member Avatar for MrHatchi87

I'm stuck with this one. I need help drawing a tree in C++. Weird right? I've posted the parameters below and my response has my code so far.

Write a program that draws a pine tree of a specified height
How tall should the tree be?: 6

  1. Prompt the user for the height of the cone of the tree

  2. Read the height from the keyboard into a variable named height

  3. If the cone height is less than 3 or greater than 15 (3 is okay and 15 is okay), then the program prints an error message and terminates

  4. Test it
    a. prompt and read the height
    b. test for height being out of bounds
    c. draw the base
    d. draw the trunk (no spaces between the sides) one half the height of the cone
    e. draw the cone

Hints
• Use three for-loops to draw the cone: two loops nested in side an outer loop:

• The outer for-loop moves the cursor from the top of the cone to the bottom; use “level” as the loop control variable and treat “height” as a constant

• Use one nested for-loop to move the cursor from the left edge of the screen to the left side of the cone

• Use the second nested for loop to move from the left side of the cone to the right side of the cone. See the table of escape sequences in chap 2 to see how to draw a back-slash character

• Note that there is not a space between the sides of the cone at the peak

• Use one for-loop to draw the base of the cone

• Note that there is not a blank line between the sides of the cone and the base

• Use two for-loops, one nested inside the other, to draw the trunk

• The outer loop moves the cursor from the top of the trunk to the bottom

• The nested loop moves the cursor from the left edge of the screen to the left side of the trunk

• Note that there is not a space between the two sides of the trunk

This is what I have so far; I don't know where to go with this one; Any guideance is appreciated

#include <iostream>
using namespace std;

int main()
{
 int height;
 int level;
 cout << "Enter the height of the tree: " << endl;
 cin >> height;

 if (height < 3)
 {
  cout << "Integer too small " << endl;
  exit (0);
 }
 else if (height > 15)
 {
  cout << "Integer too large" << endl;
  exit (0);
 }
 else 
 {
  cout << '\\' * level << endl;
 }
 return 0;
}

Don't use exit(0). Use return 1.

I don't understand the problem you are having. The instructions you posted literally tells you all the code you need to use, most of the variable names and what they are for.

The only thing you have to figure out is
a) how many SPACEs are needed before each level of the tree
b) how many SPACEs between the two sides
Simple subrtaction for that.

Draw it out on a piece of paper (graph paper if you have it) and count.
Then convert those counts into an equation.

Hint1: Work only on the left side of the tree first.
Hint2: Add the right side after you have the left side correct.

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.