December 6, 2011
A little C++ application that attempts to draw a Christmas tree to get you in to the festive spirit. Random colour generation and a random item on top… what more could you ask for? A decent program? yeah I guess so…
PS. I wasn’t allowed to use parameters so don’t shoot me for using global variables!
#include <iostream> //for cin >> and cout << #include <cassert> //for assert #include <iomanip> //for endl #include <windows.h> //for colour using namespace std; //declare constants: symbols used for the drawing const char BLANK( ' '); const char LEAF( '#'); const char WOOD( '|'); const char EOL( '\n'); //end of line symbol //declare variables int treeHeight; //height of the tree int branchLine; //loop counter for each line of the foliage int numSpaces(1); int numLeaves(1); int trunkSpaces(0); bool reDraw(false); int main() //draw Xmas tree { //Prototypes void getValidHeight(); void drawBranches(); void drawTrunk(); void reDrawTree(); void drawStar(); cout << "Jonathan Collinge - Nov 2011" << EOL << EOL; do { numSpaces = 1; //Reinitialize variables for repetition numLeaves = 1; trunkSpaces = 0; bool reDraw = false; getValidHeight(); //Promt user input cout << EOL; cout << EOL; drawStar(); drawBranches(); drawTrunk(); cout << EOL; reDrawTree(); //Redraw the chistmas tree? } while(reDraw == true); //Test for redraw cout << EOL; system( "PAUSE"); //hold the screen until a key is pressed return( 0); } void getValidHeight() //get valid height for the tree { //assert( TO BE COMPLETED ); cout << "Enter the size of the tree (4-20): "; cin >> (treeHeight); while (treeHeight < 4 || treeHeight > 20) // Validate input { cout << "ERROR: Invalid height! Enter the size of the tree (4-20): "; cin >> (treeHeight); } } void drawBranches() //draw foliage { assert( treeHeight >= 4 && treeHeight <= 20); numSpaces = (treeHeight - 1); // set numSpace dependant on user input void drawABranch(); for ( branchLine = 1; branchLine <= (treeHeight - 2); ++branchLine) drawABranch(); //draw one line of foliage } void drawABranch() //draw one line of foliage { srand((unsigned) time(NULL)); //Seed Random function for (int i(1); i <= numSpaces; i++) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0); //Set text colour to black cout << BLANK; } for (int j(1); j <= numLeaves; j++) { int randomColour(( rand()%10)+6 ); //generate random colour SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), randomColour); //Set text to random colour cout << LEAF; } cout << EOL; numLeaves = numLeaves+ 2; //Set the number of leaves needed on the next branch to +2 numSpaces--; //Reduce number of blank spaces on next branch } void drawTrunk() //draw trunk { assert( numSpaces == 1 ); trunkSpaces = (treeHeight-1); for (int k(1); k <= trunkSpaces; k++) { //prints the blank characters to format output SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0); //set text colour to black cout << BLANK; } SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6); //set text colour to brown cout << WOOD << EOL; //output base (level1) for (int a(1); a<= trunkSpaces; a++) { //prints the blank characters to format output SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0); cout << BLANK; } SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6); cout << WOOD << EOL; //output base (level2) } void drawStar() { //draws a randomly selected character at the top of the tree int starSpaces = (treeHeight-1); //initialize star location for (int x(1); x <= starSpaces; x++) //add spaces to format output { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0); cout << BLANK; } int randomColour( rand()%100 ); //set new random colour SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), randomColour); //set text colour to random colour int X = 1; int M = 2; int A = 3; int S = 4; //declare min and max constant for random boundaries const int MAX = 5; const int MIN = 1; //initialize z to a number between 1 and 5 int z((rand()%(MAX-MIN))+MIN); char decor; //Test the outcome of the random function against 5 different cases switch (z) { case 1 : { decor = '*';} break; case 2: { decor = '&';} break; case 3: { decor = '@';} break; case 4: { decor = 'O';} break; default: { decor = '^';} break; } //output result cout << decor << EOL; } void reDrawTree() { //Allow user to draw multiple trees //set text colour to grey SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); //declare local variable char response; cout << "\n\n Do you wish to draw another tree? <Y/N>: "; cin >> response; //validate input while (response != 'n' && response != 'N' && response != 'y' && response != 'Y') { cout << "\nERROR: Invalid Input! Do you wish to draw another tree? <Y/N>: "; cin >> response; } //select path if (response == 'Y' || response == 'y') { reDraw = true; system("cls"); //clear console } else { reDraw = false; } }
Reply