/* Example jackO.cpp shinnerl@ucla.edu 10/26/2001 ----------------------------------------------------- Print a jack-o-lantern as a collection of isosceles right triangles. The user can specify i) the fill characters for the eyes, nose, and teeth, ii) the heights of the eyes, nose, and teeth, iii) the left-side offset for the figure, but defaults are provided for all of these. For simplicity, the triangles are all right-isosceles oriented with the right angle in the lower left corner. */ #include #include #include #include using namespace std; void printTriangles( int h=4, int N=1, char fill = 'X', int offset=0, int spacing=0){ for ( int i = 0; i < h; ++i ) // i is the row counter { for (int j = 0; j < offset; ++j) // left offset spacing cout << ' '; for (int k = 0; k < N; ++k){ // print N rows, one for each triangle. for (int j = 0; j <= i; ++j) // Print the desired fill character. cout << fill; for (int j = i+1; j < h; ++j) // Fill the rest of this rectangle with cout << ' '; // spaces. for (int j = 0; j < spacing; ++j) // Print the spacing in this row cout << ' '; // between adjacent triangles. } cout << endl; } } void printMenu(){ cout << "Jack-o-lantern program options:\n\n" << "0 to stop customizing and draw a picture\n" << "1 to set the height of the eyes\n" << "2 to set the height of the nose\n" << "3 to set the height of the teeth\n" << "4 to set the fill character for the eyes\n" << "5 to set the fill character for the nose\n" << "6 to set the fill character for the teeth\n" << "7 to set the spacing between the eyes\n" << "8 to set the number of teeth\n" << "9 to set the left-side offset spacing\n" << "10 to view this menu again.\n\n"; } int main(){ int input; int nTeeth = 7; int eyeSpacing = 4; int eyeHeight = 5; int noseHeight = 4; int teethHeight = 3; char eyeFill = '*'; char noseFill = 'X'; char teethFill = '^'; int leftOffset = 10; printMenu(); do { cout << "Please enter a selection (0-10): "; cin >> input; if (input == 10){ printMenu(); } else if (input == 9){ cout << "Enter the left-side offset spacing (integer): "; cin >> leftOffset; } else if (input == 8){ cout << "Enter the number of teeth: "; cin >> nTeeth; } else if (input == 7){ cout << "Enter the spacing between the eyes: "; cin >> eyeSpacing; } else if (input == 6){ cout << "Enter the fill character for the teeth: "; cin >> teethFill; } else if (input == 5){ cout << "Enter the fill character for the nose: "; cin >> noseFill; } else if (input == 4){ cout << "Enter the fill character for the eyes: "; cin >> eyeFill; } else if (input == 3){ cout << "Enter the height of the teeth: "; cin >> teethHeight; } else if (input == 2){ cout << "Enter the height of the nose: "; cin >> noseHeight; } else if (input == 1){ cout << "Enter the height of the eyes: "; cin >> eyeHeight; } } while (input); //void printTriangles(int h=4,int N=1,char fill='X',int offset=0,int spacing=0); printTriangles(eyeHeight, 2, eyeFill, leftOffset+teethHeight, eyeSpacing); cout << endl; printTriangles(noseHeight, 1, noseFill, leftOffset+eyeHeight+teethHeight); cout << endl; printTriangles(teethHeight, nTeeth, teethFill, leftOffset); return 0; }