无论我做什么(不同的错误),我的功能似乎都不起作用。我该如何解决这个问题?我知道我可以把所有东西放在int main中,但是我需要输入一个函数。
编辑:该函数应该根据用户输入的数字输出一些模式。错误消息是(函数的参数太多)(参数太少)或(s1未声明)
#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;
}
答案 0 :(得分:0)
仅在main中声明number
,并且仅在您的函数中使用此参数。移动函数内其余变量的声明。更好的是,将功能分成多个独立的功能。