Guest User

Untitled

a guest
Jan 9th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | None | 0 0
  1. /*
  2. * main.cpp
  3. * Christmas Tree V5
  4. *
  5. * Created by Julius Zitzmann on 28.12.2017.
  6. * Copyright © 2017 Julius Zitzmann. All rights reserved.
  7. *
  8. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  9.  
  10. #include <iostream>
  11. #include <random>
  12. #include <Windows.h>
  13.  
  14. using namespace std;
  15.  
  16. short main()
  17. {
  18.  
  19. // ===== Variable Definition =========================
  20.  
  21. char cAgain { 'Y' } ;
  22. unsigned short usHeight { 0 } ;
  23. unsigned short usWidth { 1 } ;
  24. unsigned short usWidthMax { 0 } ;
  25.  
  26. random_device rdRandom;
  27. mt19937 mtRandom(rdRandom());
  28.  
  29. // ===== END Variable Definition =========================
  30.  
  31.  
  32. // ===== Preparation =========================
  33.  
  34. // This loop will be executed at least once. It will iterate if the user wants to build another tree.
  35. do
  36. {
  37.  
  38. // Clear the screen for a clean output.
  39. system("cls");
  40.  
  41. // Reset the variables.
  42. cAgain = 'Y' ;
  43. usHeight = 0 ;
  44. usWidth = 1 ;
  45. usWidthMax = 0 ;
  46.  
  47. // ===== END Preparation =========================
  48.  
  49.  
  50. // ===== Input =========================
  51.  
  52. // This loop will iterate infinitely until the user input is valid.
  53. while (1)
  54. {
  55.  
  56. // Set the output text color to bright white.
  57. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
  58.  
  59. // Ask the user for the height of the tree.
  60. cout << "Please enter the height of the christmas tree (5-80): ";
  61. cin >> usHeight;
  62.  
  63. // If the input fails then clear the input buffer.
  64. if (cin.fail())
  65. {
  66. cin.clear();
  67. cin.ignore(32767, '\n');
  68. }
  69.  
  70. // If the input is valid (height is between 5 and 80) then break out of the loop.
  71. if (usHeight >= 5 && usHeight <= 80)
  72. break;
  73.  
  74. // If the input is not valid then output an error message and iterate the loop.
  75. else
  76. cerr << "The input you just made is not valid, please try again!\n"
  77. << '\n';
  78. }
  79.  
  80. cout << '\n';
  81.  
  82. // ===== END Input =========================
  83.  
  84.  
  85. // ===== Output =========================
  86.  
  87. // Determine the maximum width the tree will have.
  88. usWidthMax = (usHeight * 2 - 1) - ((usHeight - 1) / 5) * 6;
  89.  
  90. if (usHeight % 5 == 1)
  91. usWidthMax = usWidthMax + 4;
  92.  
  93. if (usHeight % 5 == 2)
  94. usWidthMax = usWidthMax + 2;
  95.  
  96. // This loop outputs a certain number of spaces, 'x's, 'O's, 'I's and a new line for each height.
  97. for (unsigned short usTemp{ 1 }; usTemp <= usHeight; usTemp++, usWidth = usWidth + 2)
  98. {
  99.  
  100. // This loop outputs a certain number of spaces to center the tree.
  101. for (short sTemp{ (usWidthMax - usWidth) / 2 }; sTemp > 0; sTemp--)
  102. cout << ' ';
  103.  
  104. // This loop outputs an 'x' for each width, if not an 'O' or an 'I' because of the random integer.
  105. for (unsigned short usTemp2{ usWidth }; usTemp2 > 0; usTemp2--)
  106. {
  107.  
  108. // There is a 12,5 % chance that it is a special character.
  109. if (mtRandom() % 8 == 0)
  110. {
  111.  
  112. // There is a 50 % chance that it is a red 'O'.
  113. if (mtRandom() < 2147483648)
  114. {
  115.  
  116. // Set the output text color to red.
  117. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
  118. cout << 'O';
  119. }
  120.  
  121. // There is a 50 % chance that it is a light yellow 'I'.
  122. else
  123. {
  124.  
  125. // Set the output text color to light yellow.
  126. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14);
  127. cout << 'I';
  128. }
  129. }
  130.  
  131. // There is a 87,5 % chance that it is a normal character.
  132. else
  133. {
  134.  
  135. // Set the output text color to light green.
  136. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
  137. cout << 'x';
  138. }
  139. }
  140.  
  141. cout << '\n';
  142.  
  143. // If five lines were drawn then start a new block.
  144. if (usTemp % 5 == 0)
  145. usWidth = usWidth - 6;
  146. }
  147.  
  148. // This loop outputs three lines of centered 'H's.
  149. for (unsigned short usTemp{ 3 }; usTemp > 0; usTemp--)
  150. {
  151.  
  152. // This loop outputs a space for each width/2-1 to center the trunk.
  153. for (short sTemp{ usWidthMax / 2 }; sTemp > 0; sTemp--)
  154. {
  155. cout << ' ';
  156. }
  157.  
  158. // Set the output text color to yellow.
  159. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6);
  160. cout << "H\n";
  161. }
  162.  
  163. cout << '\n';
  164.  
  165. // ===== END Output =========================
  166.  
  167.  
  168. // ===== Loop control =========================
  169.  
  170. // Set the output text color to bright white.
  171. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
  172.  
  173. // Ask the user if he wants to build another tree.
  174. cout << "Do you want to build another tree? [Y/n] ";
  175. cin >> cAgain;
  176.  
  177. // If the input fails then clear the input buffer.
  178. if (cin.fail())
  179. {
  180. cin.clear();
  181. cin.ignore(32767, '\n');
  182. }
  183.  
  184. // If the user entered 'Y' or 'y' then tell the user that the application will iterate.
  185. if (cAgain == 'Y' || cAgain == 'y')
  186. {
  187. cout << "Okay let's build another tree! Ready?\n";
  188. system("pause");
  189. }
  190.  
  191. // If the user entered 'N' or 'n' then tell the user that the application will close.
  192. else if (cAgain == 'N' || cAgain == 'n')
  193. {
  194. cout << "Okay, Bye!\n";
  195. system("pause");
  196. }
  197.  
  198. // If the user entered something else then tell the user that the application will close.
  199. else
  200. {
  201. cout << "Interpreting inaccurate answer as \"No\". Bye!\n";
  202. system("pause");
  203. }
  204. }
  205.  
  206. // Iterate the loop if the user entered 'Y' or 'y' before.
  207. while (cAgain == 'Y' || cAgain == 'y');
  208.  
  209. // ===== END Loop control =========================
  210.  
  211.  
  212. // Exit the application with code 0.
  213. return 0;
  214. }
Advertisement
Add Comment
Please, Sign In to add comment