2013-10-17 55 views
0

所以我有这样的代码:如何将我的数组发送到另一个函数? C++

// 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; 
     } 

    } 
} 

我想checkVictorySum能够使用二维数组的游戏。我不只是检查同一个函数中的所有内容,在我们的任务中我们需要有3个函数。

任何帮助将不胜感激。 //Björn

+0

您将收到的错误消息将同样赞赏。 – WhozCraig

+0

除了我在下面的答案中的猜测,你遇到了什么问题?我建议你阅读[Stack Overflow问题清单](http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist),它会帮助你写出更好的问题。 –

+0

我建议简化您的方案。尝试制作一个非常简单的函数,它接受二维数组并从同样简单的主程序中调用该函数。在相同的代码中,除了“传递数组”问题之外,还有“游戏”代码对初学者来说可能是一个挑战。 – Jaywalker

回答

0

我猜你会得到关于隐式声明函数的错误,以及函数不符合它们的声明。原因在于你使用C++中的所有东西需要在之前声明为

在你的情况下,这可以通过在函数定义被调用之前,或者通过创建函数原型(这是函数的声明)来完成。

0

您的问题无关,在绕过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); 
} 

注意,作为函数参数含义值都将丢失的尺寸使用时数组衰变为指针。因此,您必须知道阵列的尺寸,或者将尺寸与阵列一起传递至函数

0

您需要将函数原型移出main()的主体。

int bankDeposit(); 
void game(); 
void checkVictorySum(int gameFieldCheck[3][3]); 

int main() 
{ 
    ... 
} 
相关问题