Drawing an Image in C++

Hey there, I appreciate all the help I can get from all you users :)
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


I'm not sure where to start aside from the basics of include and main
Last edited on
This is what I have so far:

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
#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;
}
Anyone willing to help me with the loops? I seem to be stuck
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
#define HEIGHT 15
#include <iostream>
using namespace std;

int main()
{
 int height;
 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);
 }
 
for (int H = 0; H < HEIGHT; H++)
	{
		for (int S = 0; S < HEIGHT- H - 1; S++)
			cout << ' ';

		for (int BackSlash = 0; BackSlash < 2 * H + 1; BackSlash++)
			cout << '\\';

		cout << endl;
	}


 return 0;
}



this. is. evil
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
42
43
44
45
46
47
48
49
50
51

#include <iostream>
using namespace std;

int main()
{
 int height;
 cout << "How tall should the tree be: " << endl;
 cin >> height;
 

 if (height < 3)
 {
  cout << "Cone can't be less than 3" << endl;
  return 1;
 }
 else if (height > 15)
 {
  cout << "Cone can't be greater than 15" << endl;
  return 1;
 }

 for (int H = 0; H < height; H++)
 {
 
  for(int S = 0; S < height - H - 1; S++)
  
   cout << ' ';

   for(int backslash = 0; backslash < 2 * H + 1; backslash++)
   
    cout << '\\';
    cout << endl;
 }
  
  for (int H = 0; H < height; H++)
  {
  
   for(int S = 0; S < height - H - 1; S++)
    cout << ' ';
   

   for (int Trunk = 0; Trunk < 2 * H + 1; Trunk++)
    cout << '|';
   cout << endl;
  }
  
 
 return 0;
}


not sure what i'm doing wrong
Topic archived. No new replies allowed.