如何在C ++中打印星号矩形 [英] How to print a rectangle of asterisks in C++

查看:74
本文介绍了如何在C ++中打印星号矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人告诉我要用户输入一个矩形要打印多少行和多少列,以及他们想要用什么符号.我不知道如何做到这一点,我所有的谷歌搜索只让我打印了一行.方向指示行应为3,列应为7,字符为"$".我仍然是初学者,所以请放轻松.这就是我所拥有的:

I have been told to ask the user to input how many rows and columns for a rectangle they would like to print and in what symbol they want it. I am unaware as to how to do this and all my google searching only got me as far as printing one row. Directions dictate that the rows should be 3 and the columns should be 7 with the character '$'. I'm still a beginner so please go easy on me. This is what I have:

#include <iostream>
#include <iomanip>

using namespace std;

void PrintChar(int row = 5, int column = 10, char symbol = '*');

int main()
{
    int rows, columns;
    char symbol;

    cout << "How many rows and columns do you want, and with what symbol (default     is *) ?" << endl;
    cin >> rows >> columns >> symbol;

    PrintChar(rows, columns, symbol);

}

void PrintChar(int row, int column, char symbol)
{

    for (int y = 1; y <= column; y++)
    {
        cout << symbol;
}

那会打印出完整的符号行,这就是我的想法停止的地方.如果您可以在最后几行中为我提供帮助,将不胜感激.

That prints out a full line of the symbol and that's where my thinking stops. If you could help me with the final rows, that would be greatly appreciated.

推荐答案

这应该可以解决问题.添加了换行符,使其看起来像一个矩形.

This should do the trick. Added a newline to make it look like a rectangle.

#include <iostream>
#include <iomanip>

using namespace std;

void PrintChar(int row = 5, int column = 10, char symbol = '*');

int main() {

    int rows, columns;
    char symbol;

    cout << "How many rows and columns do you want, and with what symbol (default     is *) ?" << endl;
    cin >> rows >> columns >> symbol;

    PrintChar(rows, columns, symbol);

    return(0);

}

void PrintChar(int row, int column, char symbol) {
    for (int y = 1; y <= column; y++) {
        for (int x = 1; x <= row; x++) {
            cout << symbol;
        }
        cout << endl;
    }
}

这篇关于如何在C ++中打印星号矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆