让这个领域重演?

时间:2012-11-09 16:23:23

标签: c++

这是我对compsci的任务,并且我在如何让星球'*'重复用户想要多次的最后阶段陷入困境。

编写打印出一系列星形字段的程序(即“*”)。每个字段将有n行和m列。允许用户输入行数(至少1但不超过5)和列(至少5但不超过50),以及字段数(至少3,但不超过10) 。每个字段必须用3个完整的空行分隔。

您的程序必须包含至少两个函数:一个用于从用户获取输入数据,另一个用于绘制每个字段。使用for循环来构造字段,以及打印出多个字段。请勿使用“”字符串。相反,打印出个人“”。

#include <iostream>

using namespace std;

void displayField(int numrows, int numcolums);

void getData (int numrows, int numcolumns, int numfields);

const char c = '*';



int main(void)
{
    int numrows, numcolumns, numfields;

    cout << "Welcome to the banner creation program!" << endl;

    cout << "Enter the number of rows (1 - 5) --> ";
    cin >> numrows;

    if(numrows<1 || numrows>5){
        cout << "Your entered value is outside the range!" << endl;
        cout << "Program will now halt..." << endl;
        exit(0);}


    cout << "Enter the number of columns (5 - 50) --> ";
    cin >> numcolumns;

    if(numcolumns<5 || numcolumns>50){
        cout << "Your entered value is outside the range!" << endl;
        cout << "Program will now halt..." << endl;
        exit(0);
    }

    cout << "Enter the number of rows (3 - 10) --> ";
    cin >> numfields;

    if(numfields<3 || numrows>10){
        cout << "Your entered value is outside the range!" << endl;
        cout << "Program will now halt..." << endl;
        exit(0);
    }
for(int i=1; i<=numrows; i++){
    for (int j=1; j<=numcolumns; j++)
            cout << c;
        cout <<endl;

}


}

1 个答案:

答案 0 :(得分:0)

打破局面......

1)对于每个字段,都有很多行。

2)每行有很多列

3)每列都有一个字符'*'

现在这样写,我们知道我们需要有3个不同的循环全部嵌套。但是存在限制。

在每行结束时,我们需要开始一个新行。

在每个字段的末尾,我们需要三个空行。

for (int i = 0; i < numFields; i++) {
  for (int j = 0; j < numRows; j++) {
    for (int k = 0; k < numColumns; k++) {
      cout << c;
    }
    cout << endl;
  }
  cout << endl << endl << endl;
}

此外,你应该仔细检查你的工作。我注意到你在错误的地方使用了一些变量提示提示