Arrow Generator

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.9 kB
2
Indexable
#include <iostream>
using namespace std;

// Prototypes
void print_blank_line(char arrow_char, int arrow_number);
void print_char(char c, int n);

int main()
{
    int number = 0;
    char c = ' ';
    cout << "Enter Number: ";
    cin >> number;
    
    if (number <= 1)
    {
        cout << "Number must be greater than 1";
        return 1;
    }
    
    cout << "Enter Character: ";
    cin >> c;
    
    // First line
    print_blank_line(c, number);
    
    // Arrow part
    int first_spaces = number - 2;
    for (int i = 0; i < number-1; i++) // For every line in the arrow part
    {
        int second_spaces = i;
        print_char(' ', first_spaces); // First spaces
        cout << c; // First character
        print_char(' ', second_spaces); // Spaces before second character
        cout << c; // Second character (middle)
        print_char(' ', second_spaces); // Spaces after second character
        cout << c; // Third character
        print_char(' ', first_spaces); // Last spaces (technically not required, since we're doing a newline anyway)
        
        first_spaces--; // For the next line, decrease the first and last spaces by 1
        cout << "\n"; // newline
        
        // Since at line 31, second spaces (before and after middle character) are equal to i, they increase by 1.
    }
    
    // Trailing lines (number of lines is equal to the inputted number)
    for (int i = 0; i < number; i++)
    {
        print_blank_line(c, number);
    }
}

// Prints the default line (line with one character)
void print_blank_line(char arrow_char, int arrow_number)
{
    print_char(' ', arrow_number-1);
    cout << arrow_char;
    print_char(' ', arrow_number-1);
    cout << '\n';
}

// Prints a character 'n' times
void print_char(char c, int n)
{
    for (int i = 0; i < n; i++)
    {
        cout << c;
    }
}