Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program that gets two inputs an integer and a character. Use repetit

ID: 3675072 • Letter: W

Question

Write a C++ program that gets two inputs an integer and a character. Use repetition control structures better known as "loops" to output an outline of a triangle shape composed of the character and the width specified by the integer. If the input is an even number, it should be increased to the next odd number. Use meaningful variable names, nested loop statements with proper indentation, appropriate comments, and good prompting messages.

For example, if the integer is 11 and the character is an asterisk (*), the triangle shape would look like this:

Sample screen output 1:

Input Validation: Ensure that input used for the width of the triangle does not exceed the screen display area and do not accept negative values for the variables used.

         Enter a value to represent the base of a triangle shape (not to exceed 80): 11          Enter the character to be used to generate the outline of a triangle shape (for eg., #, * $): *  
       *      * *     *   *    *     *   *       *  ***********  
  Do you want to quit the program? (type n for no or q to quit): q  

Explanation / Answer

#include <iostream>
#include <sstream>
#include <string>

#define NUM_ROWS 11
#define MAX_SYMBOLS 80
int main()
{
    std::string input;
    std::stringstream myStream;
    unsigned short numBase;
    char symbol;
    for (;;)
    {
        std::cout << "Enter a value for the size of the base of the pyramid: ";
        std::getline(std::cin, input);

        std::stringstream myStream(input);

        if (myStream >> numBase && numBase <= MAX_SYMBOLS)
            break;
        std::cout << std::endl << "Invalid number. Number must be positive and below " << MAX_SYMBOLS << " . Please try again." << std::endl;
    }
  
    for(;;)
    {
        std::cout << "Please enter a symbol to build the pyramid with: like * $ # @.... ";
        std::getline(std::cin, input);

        if (input.length() == 1 )
        {
            symbol = input[0];
            break;
        }
        std::cout << std::endl << "Invalid character, please try again." << std::endl;
    }

    for (int row = 0; row < NUM_ROWS; row++)
    {
        int numSymbols = 2 * row - 1;
        int numSpaces = (numBase - numSymbols)/2 ;


       // for (int i = 0; i < numSpaces; ++i)
           // std::cout << ' ';

        for (int i = 0; i < numSymbols; ++i)
            std::cout<<" " << symbol;
            //numSymbols = numSymbols-1;

        std::cout << std::endl;
    }

    std::cin.get();
    return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote