Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我怎样做一个函数?_C++ - Fatal编程技术网

C++ 我怎样做一个函数?

C++ 我怎样做一个函数?,c++,C++,无论我做什么,我的函数似乎都不起作用(有不同的错误)。我如何解决这个问题?我知道我可以把所有的东西都放在int main中,但是我需要放在函数中 编辑:该函数应该根据用户输入的数字输出一些模式。错误消息为(函数参数太多)(参数太少)或(s1未声明) #包括 #包括 使用名称空间std; 无效模式(整数编号、整数s1、整数s2、整数hs1、整数hs2、整数hs3、整数cb1、整数cb2、整数d1、整数d2、整数d3、整数d4、整数d5、整数sv1、整数sv2、整数sh1、整数sh2、整数sd1、整

无论我做什么,我的函数似乎都不起作用(有不同的错误)。我如何解决这个问题?我知道我可以把所有的东西都放在int main中,但是我需要放在函数中

编辑:该函数应该根据用户输入的数字输出一些模式。错误消息为(函数参数太多)(参数太少)或(s1未声明)

#包括
#包括
使用名称空间std;
无效模式(整数编号、整数s1、整数s2、整数hs1、整数hs2、整数hs3、整数cb1、整数cb2、整数d1、整数d2、整数d3、整数d4、整数d5、整数sv1、整数sv2、整数sh1、整数sh2、整数sd1、整数sd2、整数sdur1、整数sdLL2、整数sdLr1、整数sdLr2、整数TUL1、整数TUR2、整数TLL2、整数TLR1、整数TLR2、整数TLRS、整数lb1)
{
//方格

cout只能在main中声明
number
,并且只能在函数中使用此参数。将函数中其余变量的声明移动到函数中。更好的是,将函数拆分为多个独立函数。

代码太多。没有解释函数应该做什么。没有解释问题是什么@RSahu:好的,我修正了。请发一个帖子。你应该准确地记录所有这些参数。。
#include <iostream>
#include <string>
using namespace std;

void cpppatterns(int number, int s1, int s2, int hs1, int hs2, int hs3, int cb1, int cb2, int d1, int d2, int d3, int d4, int d5, int sv1, int sv2, int sh1, int sh2, int sd1, int sd2, int sdur1, int sdur2, int sdLL1, int sdLL2, int sdLr1, int sdLr2, int TUL1, int TUL2, int TUR1, int TUR2, int TURS, int TLL1, int TLL2, int TLR1, int  TLR2, int TLRS, int lb1)
{

//square

cout << endl << "SQUARE:" << endl;
for (s1 = 0; s1 < number; s1++){
    s2 = 0;
    cout << "   ";
    do {
        cout << "*";
        s2++;
    } while (s2 < number);
    cout << endl;
}

// hollow square :

cout << endl << "HOLLOW SQUARE:" << endl;
for (hs1 = 0; hs1 < number; hs1++){
    hs2 = 0;
    hs3 = 0;
    cout << "   ";
    if (hs1 == 0 || hs1 == number - 1){
        do {
            cout << "*";
            hs2++;
        } while (hs2 < number);
    }
    else {
        do {
            if (hs3 == number - 1 || hs3 == 0){
                cout << "*";
            }
            else {
                cout << " ";
            }
            hs3++;
        } while (hs3 < number);
    }
    cout << endl;
}


// checker-board :

cout << endl << "CHECKER-BOARD:" << endl;
for (cb1 = 0; cb1 < number; cb1++){
    cb2 = 0;
    cout << "   ";
    do {
        if (cb1 % 2 == 0){
            if (cb2 % 2 == 0){
                cout << "*";
            }
            else{
                cout << " ";
            }
        }
        else{
            if (cb2 % 2 != 0){
                cout << "*";
            }
            else{
                cout << " ";
            }
        }
        cb2++;
    } while (cb2 < number);
    cout << endl;
}

//diamond:

cout << endl << "DIAMOND:" << endl;
d2 = number;
d4 = 1;

for (d1 = 1; d1 <= number; d1++){
    cout << "   ";
    d3 = 0;
    d5 = 0;
    do {
        cout << " ";
        d3++;
    } while (d3 < d2);
    do {
        cout << "*";
        d5++;
    } while (d5 < d4);
    d4++; d4++;
    d2--;
    cout << endl;
}
d2 = 1;
d4 = d4 - 4;
for (d1 = 1; d1 < number; d1++){
    cout << "   ";
    d2++;
    d3 = 0;
    d5 = 0;
    do {
        cout << " ";
        d3++;
    } while (d3 < d2);
    do {
        cout << "*";
        d5++;
    } while (d5 < d4);
    d4--; d4--;
    cout << endl;
}

// same # vertically

cout << endl << "SAME #S VERTICALLY:" << endl;
for (int sv1 = 1; sv1 <= number; sv1++) {
    cout << "   ";
    for (int sv2 = 1; sv2 <= number; sv2++){
        cout << sv2;
    }
    cout << endl;
}

// same # horizontally 

cout << endl << "SAME #S HORIZONTALLY:" << endl;
for (int sh1 = 1; sh1 <= number; sh1++) {
    cout << "   ";
    for (int sh2 = 1; sh2 <= number; sh2++){
        cout << sh1;
    }
    cout << endl;
}
// same # DIAGONALLY ul


cout << endl << "SAME #S DIAGONALLY UPPER LEFT:" << endl;
for (int sd1 = 1; sd1 <= number; sd1++) {
    cout << "   ";
    for (int sd2 = sd1; sd2 < number + sd1; sd2++){
        cout << sd2;
    }
    cout << endl;
}

// same # DIAGONALLY UR

cout << endl << "SAME #S DIAGONALLY UPPER RIGHT:" << endl;
for (int sdur1 = 1; sdur1 <= number; sdur1++) {
    sdur2 = number - 1 + sdur1;
    cout << "   ";
    for (; sdur2 >= sdur1; sdur2--){
        cout << sdur2;
    }
    cout << endl;
}

// same # DIAGONALLY LL

cout << endl << "SAME #S DIAGONALLY LOWER LEFT:" << endl;
for (int sdLL1 = 1; sdLL1 <= number; sdLL1++) {
    cout << "   ";
    for (sdLL2 = number + 1 - sdLL1; sdLL2 <= 2 * number - sdLL1; sdLL2++){
        cout << sdLL2;
    }
    cout << endl;
}

// same # DIAGONALLY LR

cout << endl << "SAME #S DIAGONALLY LOWER RIGHT:" << endl;
for (int sdLr1 = 1; sdLr1 <= number; sdLr1++) {
    cout << "   ";
    for (sdLr2 = 2 * number - sdLr1; sdLr2 >= 1 + number - sdLr1; sdLr2--){
        cout << sdLr2;
    }
    cout << endl;
}


// triangle ul

cout << endl << "TRIANGLE, STARTING IN UPPER LEFT:" << endl;
for (int TUL1 = 1; TUL1 <= number; TUL1++) {
    cout << "   ";
    for (TUL2 = TUL1; TUL2 <= number; TUL2++){
        cout << TUL2;
    }
    cout << endl;
}


// triangle uR

cout << endl << "TRIANGLE, STARTING IN UPPER RIGHT:" << endl;
for (int TUR1 = 1; TUR1 <= number; TUR1++) {
    cout << "   ";
    for (TURS = 0; TURS < TUR1 - 1; TURS++){
        cout << " ";
    }
    for (TUR2 = number; TUR2 >= TUR1; TUR2--){
        cout << TUR2;
    }
    cout << endl;
}


// triangle LL

cout << endl << "TRIANGLE, STARTING IN LOWER LEFT:" << endl;
for (int TLL1 = 1; TLL1 <= number; TLL1++) {
    cout << "   ";
    for (TLL2 = number + 1 - TLL1; TLL2 <= number; TLL2++){
        cout << TLL2;
    }
    cout << endl;
}

// triangle LR

cout << endl << "TRIANGLE, STARTING IN LOWER RIGHT:" << endl;
for (int TLR1 = 1; TLR1 <= number; TLR1++) {
    cout << "   ";
    for (TLRS = 0; TLRS < number - TLR1; TLRS++){
        cout << " ";
    }
    for (TLR2 = number; TLR2 >= number + 1 - TLR1; TLR2--){
        cout << TLR2;
    }
    cout << endl;
}

// letter box: 

int lb2 = 'A';
cout << endl << "LETTER BOX:" << endl;
lb1 = number + 64;
for (lb2 = 0; lb2 <= number; lb2++) {
    cout << lb2;
}



}

int main() {
    int number, s1, s2, hs1, hs2, hs3, cb1, cb2, d1, d2, d3, d4, d5, sv1, sv2, sh1, sh2, sd1, sd2, sdur1, sdur2, sdLL1, sdLL2, sdLr1, sdLr2, TUL1, TUL2, TUR1, TUR2, TURS, TLL1, TLL2, TLR1, TLR2, TLRS, lb1;
cin >> number;
cpppatterns(number, s1, s2, hs1, hs2, hs3, cb1, cb2, d1, d2, d3, d4, d5, sv1, sv2, sh1, sh2, sd1, sd2, sdur1, sdur2, sdLL1, sdLL2, sdLr1, sdLr2, TUL1, TUL2, TUR1, TUR2, TURS, TLL1, TLL2, TLR1, TLR2, TLRS, lb1);

// factors:
int i;
cout << "Factors of " << number << " are: " << endl;
for (i = 1; i <= number; ++i)
{
    if (number%i == 0)
        cout << i << endl;
}
return 0;
}