0

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.

4

4 回答 4

2
  • 首先,int main()应该有一个return声明。

  • PrintChar 内部应该有 2 个嵌套的 for 循环,外部一个用于行,内部一个用于列,例如:-

    for (int x = 1; x <= rows; x++) { cout << endl; for (int y = 1; y <= columns; y++) { cout << 符号; } }

于 2013-11-11T04:42:09.517 回答
2

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

#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;
    }
}
于 2013-11-11T04:43:22.540 回答
1

使用嵌套循环可以实现这一点。

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

    for (int x = 0; x < row; x++)
    {
       for (int y = 1; y <= column; y++)
       {
           cout << symbol;
       }
       cout << endl;
    }
}
于 2013-11-11T04:47:00.477 回答
0

似乎是一个基本的星形循环练习。使用嵌套循环打印所需的模式

for(i=1; i<=n; i++)  
{  
    for(j=1; j<=m; j++)  
    {  
        cout<<"*";  
    }  
    cout<<"\n";  
}

n是行数,m是每行的列数。

于 2015-07-01T05:37:08.117 回答