Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * main.cpp
- * Christmas Tree V5
- *
- * Created by Julius Zitzmann on 28.12.2017.
- * Copyright © 2017 Julius Zitzmann. All rights reserved.
- *
- * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
- #include <iostream>
- #include <random>
- #include <Windows.h>
- using namespace std;
- short main()
- {
- // ===== Variable Definition =========================
- char cAgain { 'Y' } ;
- unsigned short usHeight { 0 } ;
- unsigned short usWidth { 1 } ;
- unsigned short usWidthMax { 0 } ;
- random_device rdRandom;
- mt19937 mtRandom(rdRandom());
- // ===== END Variable Definition =========================
- // ===== Preparation =========================
- // This loop will be executed at least once. It will iterate if the user wants to build another tree.
- do
- {
- // Clear the screen for a clean output.
- system("cls");
- // Reset the variables.
- cAgain = 'Y' ;
- usHeight = 0 ;
- usWidth = 1 ;
- usWidthMax = 0 ;
- // ===== END Preparation =========================
- // ===== Input =========================
- // This loop will iterate infinitely until the user input is valid.
- while (1)
- {
- // Set the output text color to bright white.
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
- // Ask the user for the height of the tree.
- cout << "Please enter the height of the christmas tree (5-80): ";
- cin >> usHeight;
- // If the input fails then clear the input buffer.
- if (cin.fail())
- {
- cin.clear();
- cin.ignore(32767, '\n');
- }
- // If the input is valid (height is between 5 and 80) then break out of the loop.
- if (usHeight >= 5 && usHeight <= 80)
- break;
- // If the input is not valid then output an error message and iterate the loop.
- else
- cerr << "The input you just made is not valid, please try again!\n"
- << '\n';
- }
- cout << '\n';
- // ===== END Input =========================
- // ===== Output =========================
- // Determine the maximum width the tree will have.
- usWidthMax = (usHeight * 2 - 1) - ((usHeight - 1) / 5) * 6;
- if (usHeight % 5 == 1)
- usWidthMax = usWidthMax + 4;
- if (usHeight % 5 == 2)
- usWidthMax = usWidthMax + 2;
- // This loop outputs a certain number of spaces, 'x's, 'O's, 'I's and a new line for each height.
- for (unsigned short usTemp{ 1 }; usTemp <= usHeight; usTemp++, usWidth = usWidth + 2)
- {
- // This loop outputs a certain number of spaces to center the tree.
- for (short sTemp{ (usWidthMax - usWidth) / 2 }; sTemp > 0; sTemp--)
- cout << ' ';
- // This loop outputs an 'x' for each width, if not an 'O' or an 'I' because of the random integer.
- for (unsigned short usTemp2{ usWidth }; usTemp2 > 0; usTemp2--)
- {
- // There is a 12,5 % chance that it is a special character.
- if (mtRandom() % 8 == 0)
- {
- // There is a 50 % chance that it is a red 'O'.
- if (mtRandom() < 2147483648)
- {
- // Set the output text color to red.
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
- cout << 'O';
- }
- // There is a 50 % chance that it is a light yellow 'I'.
- else
- {
- // Set the output text color to light yellow.
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14);
- cout << 'I';
- }
- }
- // There is a 87,5 % chance that it is a normal character.
- else
- {
- // Set the output text color to light green.
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
- cout << 'x';
- }
- }
- cout << '\n';
- // If five lines were drawn then start a new block.
- if (usTemp % 5 == 0)
- usWidth = usWidth - 6;
- }
- // This loop outputs three lines of centered 'H's.
- for (unsigned short usTemp{ 3 }; usTemp > 0; usTemp--)
- {
- // This loop outputs a space for each width/2-1 to center the trunk.
- for (short sTemp{ usWidthMax / 2 }; sTemp > 0; sTemp--)
- {
- cout << ' ';
- }
- // Set the output text color to yellow.
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6);
- cout << "H\n";
- }
- cout << '\n';
- // ===== END Output =========================
- // ===== Loop control =========================
- // Set the output text color to bright white.
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
- // Ask the user if he wants to build another tree.
- cout << "Do you want to build another tree? [Y/n] ";
- cin >> cAgain;
- // If the input fails then clear the input buffer.
- if (cin.fail())
- {
- cin.clear();
- cin.ignore(32767, '\n');
- }
- // If the user entered 'Y' or 'y' then tell the user that the application will iterate.
- if (cAgain == 'Y' || cAgain == 'y')
- {
- cout << "Okay let's build another tree! Ready?\n";
- system("pause");
- }
- // If the user entered 'N' or 'n' then tell the user that the application will close.
- else if (cAgain == 'N' || cAgain == 'n')
- {
- cout << "Okay, Bye!\n";
- system("pause");
- }
- // If the user entered something else then tell the user that the application will close.
- else
- {
- cout << "Interpreting inaccurate answer as \"No\". Bye!\n";
- system("pause");
- }
- }
- // Iterate the loop if the user entered 'Y' or 'y' before.
- while (cAgain == 'Y' || cAgain == 'y');
- // ===== END Loop control =========================
- // Exit the application with code 0.
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment