fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <iostream>
  4. #include <string>
  5.  
  6. //drawTriangle: Called on to draw the triangle
  7. void drawTriangle(int size, char direction, char pattern);
  8.  
  9. void ScanInputChar(std::string prompt, char &input, std::string expected)
  10. {
  11. do
  12. {
  13. std::string strInput;
  14. std::cout<<prompt;
  15. std::cin>>strInput;
  16. input = tolower(strInput.c_str()[0]);
  17. }
  18. while(expected.find(input) == std::string::npos);
  19. }
  20.  
  21. void ScanInputNum(std::string prompt, int &input, int expectedmin, int expectedmax)
  22. {
  23. do
  24. {
  25. std::string strInput;
  26. std::cout<<prompt;
  27. std::cin>>strInput;
  28. input = atoi(strInput.c_str());
  29. }
  30. while(input < expectedmin || input > expectedmax);
  31. }
  32.  
  33. int main() {
  34. //Variables which will be filled by user
  35. int size;
  36. char direction, pattern;
  37.  
  38. //Getting input from user
  39. ScanInputNum("Please enter the size of your triangle (Must be between 1-15)\n", size, 0, 15);
  40. ScanInputChar("Please enter the direction of your triangle (Either F or B)\n", direction, "fb");
  41. ScanInputChar("Please enter the pattern for your triangle (V for vertical, H for horizontal, or N for none)\n", pattern, "vhn");
  42.  
  43. printf("\n\nHere is your triangle:\n\n");
  44.  
  45. //Calling drawTirangle and sending the user input
  46. drawTriangle(size, direction, pattern);
  47.  
  48. system("pause");
  49.  
  50. return 0;
  51. }
  52.  
  53. //drawTriangle: Draws the triangle using the users parameters
  54. void drawTriangle(int size, char direction, char pattern)
  55. {
  56. int iPos = 0;
  57. int iIncrement = 0;
  58. bool bDirection = (direction == 'b'); //true if backward
  59.  
  60. if(bDirection)
  61. iIncrement = -1;
  62. else
  63. iIncrement = 1;
  64.  
  65. for(int i = 0; i < size; ++i, iPos+=iIncrement)
  66. {
  67. if(bDirection)
  68. {
  69. for(int k = size-i;k>1; k--)
  70. printf(" ");
  71. }
  72.  
  73. for(int j = 0; j != iPos + iIncrement; j+=iIncrement)
  74. {
  75. if(bDirection)
  76. printf(" ");
  77.  
  78. if(pattern == 'v')
  79. {
  80. if((j + ((i%2)*bDirection))%2==0)
  81. printf("#");
  82. else
  83. printf("*");
  84. }
  85. else if(pattern == 'h')
  86. {
  87. if(i%2==0)
  88. printf("#");
  89. else
  90. printf("*");
  91. }
  92. else
  93. {
  94. printf("*");
  95. }
  96.  
  97. if(!bDirection)
  98. printf(" ");
  99.  
  100. }
  101. printf("\n");
  102. }
  103. }
  104.  
Success #stdin #stdout #stderr 0s 3464KB
stdin
10 F V
stdout
Please enter the size of your triangle (Must be between 1-15)
Please enter the direction of your triangle (Either F or B)
Please enter the pattern for your triangle (V for vertical, H for horizontal, or N for none)


Here is your triangle:

# 
# * 
# * # 
# * # * 
# * # * # 
# * # * # * 
# * # * # * # 
# * # * # * # * 
# * # * # * # * # 
# * # * # * # * # * 
stderr
sh: 1: pause: not found