2013-10-17 80 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() 
{ 
    ... 
} 
相關問題