Console Tree

I saw a homework assignment about drawing trees in the console window so i thought Id try my hand at making one.

Here you go!

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
	Patricks tree creator.
	Draws a tree in the console window!
	Patrick O'Grady 09.22.2012.1949
*/
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <windows.h>

using namespace std;

//create a pseuodo random tree for the amusment of the user.
const char* RandomTree(int Seed)
{
	srand(Seed);

	int RandomTreeType = rand() % 10 + 1;

	switch(RandomTreeType)
	{
	case 0:
		return "Pinus Armandii";
	case 1:
		return "Saylix Babylonica";
	case 2:
		return "Abies Firma";
	case 3:
		return "Cornus Kousa";
	case 4:
		return "Davidia Involucrata";
	case 5:
		return "Evodia Damiellii";
	case 6:
		return "Betula Grossa";
	case 7:
		return "Cercidiphyllum Japonicium";
	case 8:
		return "Betula Papyrifera";
	case 9:
		return "Clerodendrum Trichotomum";
	default:
		return "Corylorsis Glabrescens";
	};

	return "No Tree";
};

int main()
{
	//the height of the tree
	int h = 0;
	char Answer = 'y';

	//get a handle to the console
	HANDLE Console = 0;
	Console = GetStdHandle(STD_OUTPUT_HANDLE);

	//brighten the console up a little
	SetConsoleTextAttribute(Console, 15);
	
	cout<<"Welcome to Patricks Tree Creator!\n"<<endl;
	while(Answer == 'y')
	{
		cout<<"Please enter the height of your tree: ";
		cin>>h;
		cout<<endl;

		//set the color of the text to green to draw the tree
		SetConsoleTextAttribute(Console, 2);
		//draw the tree
		cout<<setw(h)<<"^"<<endl;
		for(int i = 1; i <= h-1; i++)
		{
			cout<<setw(h-i);
			for(int y = 0; y <= i+(i*1); y++)
			{
				cout<<"^";
			}

			cout<<endl;
		}

		SetConsoleTextAttribute(Console, 6);

		//draw the trunk and base.
		cout<<setw(h)<<"#"<<endl;
		cout<<setw(h)<<"#"<<endl;
		cout<<setw(h)<<"#"<<endl;
		cout<<setw(h)<<"#"<<endl;
		cout<<setfill('_')<<setw(h*2)<<"______"<<endl;
		cout<<setfill(' ');

		SetConsoleTextAttribute(Console, 15);
		cout<<"\nNice tree! Is that a "<<RandomTree(time(0))<<"?"<<"\nWant to make another one (y/n)?";
		cin>>Answer;
		if(Answer == 'y')
			cout<<"Need more bark for your bite huh?"<<endl;
	}

	cout<<"\nThanks for playing the tree creator!\n"<<endl;
	cout<<"Tree names taken from http://www.ces.ncsu.edu/depts/hort/consumer/factsheets/trees-new/scientific_namesa_e.html.  Thanks!\n"<<endl;
	cout<<"Press 'e' to exit: ";
	cin>>h;
	return 0;
}


I'm a newb, so this is probably a dumb question, but......
Using Code::Blocks, I get the following error when I Build it.

C:\Program Files\CodeBlocks\MinGW\downloaded\Tree\main.cpp|96|error: 'time' was not declared in this scope||

Is this a cockpit error on my part, or is this a valid bug?
It compiles on vc 2010. I need to include time.h.

Thanks.
@ redeemed: The function "time()" is exposed in the header file 'ctime'. This should be included at the top of your application.
Here is a new version of the tree builder. I included a check to see if the input is valid.
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
	Patricks tree creator.
	Draws a tree in the console window!
	Patrick O'Grady 09.22.2012.1949
*/
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <windows.h>
#include <time.h>

using namespace std;

//create a pseuodo random tree for the amusment of the user.
const char* RandomTree(int Seed)
{
	srand(Seed);

	int RandomTreeType = rand() % 10 + 1;

	switch(RandomTreeType)
	{
	case 0:
		return "Pinus Armandii";
	case 1:
		return "Saylix Babylonica";
	case 2:
		return "Abies Firma";
	case 3:
		return "Cornus Kousa";
	case 4:
		return "Davidia Involucrata";
	case 5:
		return "Evodia Damiellii";
	case 6:
		return "Betula Grossa";
	case 7:
		return "Cercidiphyllum Japonicium";
	case 8:
		return "Betula Papyrifera";
	case 9:
		return "Clerodendrum Trichotomum";
	default:
		return "Corylorsis Glabrescens";
	};

	return "No Tree";
};

int main()
{
	//the height of the tree
	int h = 0;
	char Answer = 'y';

	//get a handle to the console
	HANDLE Console = 0;
	Console = GetStdHandle(STD_OUTPUT_HANDLE);

	//brighten the console up a little
	SetConsoleTextAttribute(Console, 15);
	
	cout<<"Welcome to Patricks Tree Creator!\n"<<endl;
	while(Answer == 'y')
	{
		cout<<"Please enter the height of your tree (10-20 is a good size): ";
		
		//check if input is valid
		if(!(cin>>h))
		{
			cin.clear();
			cin.ignore(100, '\n');
			continue;
		}

		cout<<endl;

		//set the color of the text to green to draw the tree
		SetConsoleTextAttribute(Console, 2);
		//draw the tree
		cout<<setw(h)<<"^"<<endl;
		for(int i = 1; i <= h-1; i++)
		{
			cout<<setw(h-i);
			for(int y = 0; y <= i+(i*1); y++)
			{
				cout<<"^";
			}

			cout<<endl;
		}

		SetConsoleTextAttribute(Console, 6);

		//draw the trunk and base.
		cout<<setw(h)<<"#"<<endl;
		cout<<setw(h)<<"#"<<endl;
		cout<<setw(h)<<"#"<<endl;
		cout<<setw(h)<<"#"<<endl;
		cout<<setfill('_')<<setw(h*2)<<"______"<<endl;
		cout<<setfill(' ');

		SetConsoleTextAttribute(Console, 15);
		cout<<"\nNice tree! Is that a "<<RandomTree(time(0))<<"?"<<"\nWant to make another one (y/n)?";
		cin>>Answer;
		if(Answer == 'y')
			cout<<"Need more bark for your bite huh?"<<endl;
	}

	cout<<"\nThanks for playing the tree creator!\n"<<endl;
	cout<<"Tree names taken from http://www.ces.ncsu.edu/depts/hort/consumer/factsheets/trees-new/scientific_namesa_e.html.  Thanks!\n"<<endl;
	cout<<"Press 'e' to exit: ";
	cin>>h;
	return 0;
}
Last edited on
Topic archived. No new replies allowed.