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

bool readInteger(const std::string& prompt, int& value, int low, int high) {
    std::string line;
    while (std::cout << prompt && std::getline(std::cin, line)) {
        std::istringstream input(line);
        char extra = '\0';
        if (input >> value && !(input >> extra) && value >= low && value <= high) return true;
        std::cout << "Enter a whole number in [" << low << ", " << high << "].\n";
    }
    std::cerr << "Input ended before a valid integer was supplied.\n";
    return false;
}

void printTree(int height) {
    for (int row = 0; row < height; ++row) {
        for (int space = 0; space < height - row - 1; ++space) std::cout << ' ';
        for (int mark = 0; mark < 2 * row + 1; ++mark) std::cout << 'X';
        std::cout << '\n';
    }
    for (int space = 0; space < height - 1; ++space) std::cout << ' ';
    std::cout << "|\n";
}

int main() {
    int height = 0;
    if (!readInteger("Tree height (1 to 30): ", height, 1, 30)) return 1;
    printTree(height);
}
