我有两个问题。我正在显示用户定义的数组。用户输入是行数,列数,以及数组中放置符号的间隔。
我正在寻找这个结果。输入= 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");
}
答案 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是工作示例。