/* triangle.cpp -- Matt Mahoney, mmahoney@cs.fit.edu ANALYSIS (required for homework assignments) This program prompts the user to enter his/her name and prints a greeting, "Hello, name!" surrounded by a triangle of asterisks. The greeting is on the third line from the bottom, with one space on the left and one or two spaces on the right. For instance: Please enter your name: Matt * * * * * * * * * * * * * * * * Hello, Matt! * * * ********************* DESIGN (optional, recommended for large projects) To solve this problem, let us number the n lines of the triangle 0, 1, 2, ... n - 1. The greeting is on line n - 3. Line n - 1 consists of 2n - 1 asterisks with no leading spaces. Line 0 consists of a single asterisk with n - 1 leading spaces. For all other lines, line i has n - i - 1 leading spaces, a *, then 2i - 1 spaces, then another *. The number of spaces is odd, so on line n - 3 where we substitute the greeting, its length must also be odd, so we pad it with an extra space if necessary. We solve the problem with a loop going from 0 to n - 1 for each line, and, testing for the special cases of lines 0 (the top), n - 3 (the greeting), and n - 1 (the bottom). TESTING (required) Tested under SunOS 5.8 on evans.cs.fit.edu with g++ 2.95.1 and Windows Me, DJGPP 2.95.2 To compile: g++ triangle.cpp */ #include #include using namespace std; int main() { // Prompt for the user's name and construct a greeting with length odd. cout << "Please enter your name: "; string name; cin >> name; string greeting = "Hello, " + name + "!"; if (greeting.size()%2 == 0) // even? greeting += " "; // Print the triangle. const int n=greeting.size()/2 + 5; // Height of the triangle for (int i=0; i