Expand|Select|Wrap|Line Numbers
- // Program allows user to select number and type of pattern to be printed to screen
- #include <iostream>
- using namespace std;
- // Function prototypes
- void getInput(int&, char&);
- void printError(int&, char&);
- int printPattern(int, char);
- void printLastLine(int, char);
- int main()
- {
- int numRows = 0;
- char typeChar = 0;
- getInput(numRows, typeChar);
- printPattern(numRows, typeChar);
- printLastLine(numRows, typeChar);
- return 0;
- }
- //************************************************************************
- // Definition of getInput which obtains user info on how to build pattern*
- //************************************************************************
- void getInput(int& rows, char& characterSelect)
- {
- cout << "How many rows do you want in the pattern (even numbers only: 2-14)? ";
- cin >> rows;
- cout << "What character do you want the pattern made of (select from: * + # or $)? ";
- cin >> characterSelect;
- printError(rows, characterSelect);
- }
- //************************************************************************
- // Definition of printError which determines if user has entered *
- // acceptable input. *
- //************************************************************************
- void printError(int& rows, char& characterSelect)
- {
- if ((rows < 2) || (rows > 14) || (rows%2 != 0))
- {
- cout << "\nInvalid number of rows. Please retry.\n";
- cout << "\n";
- system("pause");
- system("cls");
- getInput(rows, characterSelect);
- }
- else if ((characterSelect != '*') && (characterSelect != '+') && (characterSelect != '#') && (characterSelect != '$'))
- {
- cout << "\nInvalid character. Please retry using one of the four characters given.\n";
- cout << "\n";
- system("pause");
- system("cls");
- getInput(rows, characterSelect);
- }
- }
- //************************************************************************
- // Definition of printPattern displays pattern based on user entered data*
- // and tabulates the number of characters printed on screen. *
- //************************************************************************
- int printPattern(int rows, char characterSelect)
- {
- int halfValue = rows / 2;
- for (int row = 0; row <= halfValue; row++)
- {
- for(int character = 0; character < row; character++)
- cout << characterSelect;
- cout << "\n";
- }
- for (int row2 = halfValue; row2 >= 0; row2--)
- {
- for(int character2 = 0; character2 < row2; character2++)
- cout << characterSelect;
- cout << "\n";
- }
- return rows;
- }
- //**********************************************************************
- // Definition of printLastLine function displays to the user the number*
- // of characters used to print the pattern as well as the type of *
- // character used. *
- //**********************************************************************
- void printLastLine(int numRows, char characterSelect)
- {
- int totalCharacters = 0;
- if (numRows == 2)
- totalCharacters = 2;
- else if (numRows == 4)
- totalCharacters = 6;
- else if (numRows == 6)
- totalCharacters = 12;
- else if (numRows == 8)
- totalCharacters = 20;
- else if (numRows == 10)
- totalCharacters = 30;
- else if (numRows == 12)
- totalCharacters = 42;
- else if (numRows == 14)
- totalCharacters = 56;
- cout << "The total number of " << characterSelect << "'s displayed was " << totalCharacters << ".\n";
- cout << "\n";
- }