Preppeh Preppeh - 1 month ago 11
C++ Question

Creating a box out of X's depending on the number inputted by the user

So in my C++ programming class, we have a problem where we need to write a program that asks for an integer from the user and then create a box out of X's that's side lengths are equal to the number inputted by the user.
For example, if the user inputted 5, the output would be:

XXXXX

XXXXX

XXXXX

XXXXX

XXXXX

I'm not sure how to go about this, I feel like I need to use a for loop but don't know how to structure it. Any help would be appreciated!

Answer

Simply use 2 for loops. First you have to get the input of the user. This is achived by using the standard input and the flux operator std::cin >> store_input.

Then you loop: n times for the columns and, inside, n times for the lines.

#include <iostream>

int main()
{
    int number;

    // Output.
    std::cout << "Enter a number: ";

    // Gets the input. 
    std::cin >> number;

    // For each column, process one line + return carriage
    for (int j = 0; j < number; ++j)
    {
        // For one line.
        for (int i = 0; i < number; ++i)
        {
            std::cout << 'X';
        }

        std::cout << '\n';
    }

    return 0;
} 
Comments