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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
// INCLUDE FILES
#include <iostream>
using namespace std;
int main()
{
int no_lines, // height of triangle or rectangle.
width; // width of rectangle.
short choice;
// ADDITIONAL VARIABLES
// ADD A do-while LOOP TO THE PROGRAM SO THAT THE USER CAN REPEATEDLY
// DISPLAY THE MENU, MAKE A CHOICE, AND HAVE THE APPROPRIATE STEPS FOR
// THAT CHOICE CARRIED OUT.
// THE LOOP SHOULD CONTINUE ITERATING UNTIL THE USER ENTERS 7 FOR THE MENU CHOICE.
// HAVE THE PROGRAM PRINT SEVERAL BLANK LINES AFTER EACH SHAPE IS DRAWN.
do
{
// Display menu.
cout << "THIS PROGRAM DRAWS VARIOUS SHAPES.\n" << endl;
cout << "1. Filled Triangle" << endl;
cout << "2. Filled Inverted Trianlge" << endl;
cout << "3. Unfilled Triangle" << endl;
cout << "4. Unfilled Inverted Trianlge" << endl;
cout << "5. Filled Rectangle" << endl;
cout << "6. Unfilled Rectangle" << endl;
cout << "7. Exit\n" << endl;
cin >> choice;
// ASK, READ AND VALIDATE CHOICE WITH while LOOP.
// Process choice.
switch (choice)
{
case 1: // Filled Triangle
// Ask, read and validate number of lines.
cout << "Enter the height for the filled triangle (2 - 75): ";
cin >> no_lines;
while (no_lines < 2 || no_lines > 75)
{
cout << "Number of lines has to be between 2 and 75.\n";
cout << "Enter the height for the filled triangle (2 - 75): ";
cin >> no_lines;
}
cout << endl;
// Draw shape.
// An example of a filled triangle with height 5
// *
// **
// ***
// ****
// *****
int i, j;
for (i = 1; i <= no_lines; i++)
{
for (j = 1; j <= i; j++)
cout << "*";
cout << endl;
}
break;
case 2: // Inverted filled triangle
// ASK, READ AND VALIDATE HEIGHT.
// DRAW INVERTED TRIANGLE
// AN EXAMPLE OF AN INVERTED TRIANGLE OF HEIGHT 10
// **********
// *********
// ********
// *******
// ******
// *****
// ****
// ***
// **
// *
break;
case 3: // Unfilled triangle
// ASK, READ AND VALIDATE HEIGHT.
// DRAW UNFILLED TRIANGLE
// AN EXAMPLE OF A TRIANGLE OF HEIGHT 8
// *
// **
// * *
// * *
// * *
// * *
// * *
// ********
break;
case 4: // Unfilled inverted triangle
// ASK, READ AND VALIDATE HEIGHT.
// DRAW UNFILLED INVERTED TRIANGLE
// AN EXAMPLE OF AN UNFILLED INVERTED TRIANGLE OF HEIGHT 7
// *******
// * *
// * *
// * *
// * *
// **
// *
break;
case 5:
// ASK, READ AND VALIDATE HEIGHT.
// ASKK, READ AND VALIDATE WIDTH.
// DRAW FILLED RECTANGLE
// AN EXAMPLE OF A 5 (HEIGHT) X 10 (WIDTH) FILLED RECTANGLE.
// **********
// **********
// **********
// **********
// **********
break;
case 6:
// ASK, READ AND VALIDATE HEIGHT.
// ASKK, READ AND VALIDATE WIDTH.
// DRAW UNFILLED RECTANGLE
// AN EXAMPLE OF A 4 (HEIGHT) X 8 (WIDTH) UNFILLED RECTANGLE
// ********
// * *
// * *
// ********
break;
case 7:
cout << "Exiting ...\n";
return 0;
break;
}
} while (choice >= 1 && choice <= 7);
system("pause");
return 0;
}
|