C++ 如何将数组发送到另一个函数?C++;
所以我有这个代码:C++ 如何将数组发送到另一个函数?C++;,c++,arrays,multidimensional-array,C++,Arrays,Multidimensional Array,所以我有这个代码: // ConsoleApplication2.cpp : Defines the entry point for the console application. // #include <iostream> #include <ctime> #include <stdio.h> using namespace std; int main() { int bankDeposit(); void game();
// ConsoleApplication2.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <ctime>
#include <stdio.h>
using namespace std;
int main()
{
int bankDeposit();
void game();
void checkVictorySum(int gameFieldCheck[3][3]);
int totalDeposit = 0;
int gameField;
cout << "Welcome to the slot machine, bet a sum and win money depending on the number of equal rows." << endl;
totalDeposit = bankDeposit();
game();
return 0;
}
int bankDeposit()
{
int deposit = 0;
int betAbleMoney = 0;
bool correctDeposit = false;
bool endOfFunction = false;
bool incorrectAnswer = true;
char wantToDepositMore = 'Y';
while (!endOfFunction)
{
while (wantToDepositMore == 'Y')
{
while (!correctDeposit)
{
cout << "How much money do you want to deposit? " << endl;
cin >> deposit;
if (cin.fail())
{
cin.clear();
cin.ignore();
cout << "That's an incorrect input" << endl;
}
else
{
correctDeposit = true;
}
}
betAbleMoney += deposit;
cout << "You have deposited a total of " << betAbleMoney << " Euro" << endl;
incorrectAnswer = true;
while (incorrectAnswer)
{
cout << "Do you want to deposit more money? (Y/N)" << endl;
cin >> wantToDepositMore;
if (cin.fail())
{
cin.clear();
cin.ignore();
cout << "That's an incorrect input" << endl;
incorrectAnswer = true;
}
else
{
incorrectAnswer = false;
}
if (wantToDepositMore != 'N' && wantToDepositMore != 'Y')
{
incorrectAnswer = true;
cout << "That's an incorrect input " << endl;
}
else
{
if (wantToDepositMore == 'N')
{
endOfFunction = true;
}
else
{
correctDeposit = false;
deposit = 0;
}
}
}
}
}
return betAbleMoney;
}
void game()
{
void checkVictorySum(int gameFieldCheck);
srand(time(0));
int gameField [3][3];
char mainGameField [3][3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
gameField[i][j] = rand() % 3 + 1;
}
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (gameField[i][j] == 1)
{
mainGameField [i][j] = 'x';
}
if (gameField[i][j] == 2)
{
mainGameField [i][j] = 'o';
}
if (gameField[i][j] == 3)
{
mainGameField [i][j] = '*';
}
cout << mainGameField[i][j];
}
cout << endl;
}
checkVictorySum(gameField[3][3]);
}
void checkVictorySum(int gameField[3][3])
{
int rows = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << gameField[i][j];
cout << endl;
}
}
}
//ConsoleApplication2.cpp:定义控制台应用程序的入口点。
//
#包括
#包括
#包括
使用名称空间std;
int main()
{
int银行存款();
无效游戏();
void checkVictorySum(int gameFieldCheck[3][3]);
int totalDeposit=0;
智力游戏场;
Cuth我猜你会对隐式声明的函数产生错误,而函数不匹配它们的声明。原因是C++中的所有东西都需要在使用之前声明。
在您的情况下,这可以通过在调用函数定义之前放置函数定义来实现,也可以通过制作函数原型(即函数的声明)来实现。您的问题与传递2维数组无关,因为这样做:
void func(int arr[3][3])
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
arr[i][j] = 0;
}
}
}
int main()
{
int arr[3][3] = {0};
func(arr);
}
void func(int arr[3][3])
{
对于(int i=0;i<3;i++)
{
对于(int j=0;j<3;j++)
{
arr[i][j]=0;
}
}
}
int main()
{
int arr[3][3]={0};
func(arr);
}
请注意,当用作函数参数时,数组会衰减为指针,这意味着维度值丢失。因此,您必须知道数组的大小,或者将维度随数组一起传递给函数您需要将函数原型移出main()
的主体
同样感谢您收到的错误消息。除了我在下面的答案中的猜测之外,您遇到了什么问题?我建议您阅读,它将帮助您编写更好的问题。我建议简化您的场景。尝试制作一个非常简单的函数,它接受2D数组,并从一个同样简单的主程序调用该函数。对于初学者来说,在同一代码中添加“游戏”代码和“传球数组”问题可能是一个挑战。
int bankDeposit();
void game();
void checkVictorySum(int gameFieldCheck[3][3]);
int main()
{
...
}