用户在C ++中定义的行,列输出

时间:2015-04-05 20:52:48

标签: c++ arrays

我有两个问题。我正在显示用户定义的数组。用户输入是行数,列数,以及数组中放置符号的间隔。

  1. 我无法获得默认符号****的输出。
  2. 我需要在哪里插入间隔输入来相应地调整数组?在函数内部或定义另一个函数来做到这一点?
  3. 我正在寻找这个结果。输入= 1(行),4(列),2(间隔)。输出将是**?*。

    这是我到目前为止所拥有的。

    #include <iostream>
    
    using namespace std;
    
    int rows = 0, columns = 0, interval = 0;
    char symbol;
    void Display(int rows = 0, int columns = 0, int interval = 0, char symbol = ('*'));
    
    int main()
    {
    
        cout << "Enter number of rows: ";
        cin >> rows;
    
        cout << "Enter the number of columns: ";
        cin >> columns;
    
        cout << "Enter the number of the question mark interval: ";
        cin >> interval;
    
        cout << "\n";
        cout << "How many rows do you want? " << rows << "\n";
        cout << "How many columns do you want? " << columns << "\n";
        cout << "How far between question marks? " << interval << "\n";
    
        Display(rows, columns,  interval, symbol);
    
        return 0;
    }
    void Display(int rows, int columns, int intervals, char symbol)
    {
        for (int y = 1; y <= rows; y++)
        {
            for (int x = 1; x <= columns; x++) {
                cout << symbol;
            }
            cout << endl;
        }
        system("pause");        
    }
    

1 个答案:

答案 0 :(得分:1)

问题是您从未将*分配给symbol

更改

char symbol;

char symbol = '*';

您是否知道全局变量的缺点?你越早了解这种缺点就越好。 Here是一个起点。

修改Display功能,如下所示:

void Display(int rows, int columns, int intervals, char symbol)
{
    for (int y = 1; y <= rows; y++)
    {
        for (int x = 1; x <= columns; x++) {
            if ((x % (interval + 1)) == 0)   //<--since you want after every intervals, just do a mod operation
                cout << "?";
            else
                cout << symbol;
         }
         cout << endl;
    }
    system("pause");        
}

Here是工作示例。