2017-09-24 15 views
-6
#include <iostream> 
#include <cstdlib> 
#include <cmath> 

using namespace std; 

void displayRules(); 
void play(); 
int shuffleCard(int cardPile[]); 


int main() 
{ 
    int board[26] = {0, 1, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0}; 
    int cardPile[10] = {1, 1, 2, 2, 3, 3, 4, 4, 0, 5}; 
    int player1 = 0; 
    int player2 = 0; 

    play(); 


    return 0; 
} 

void play(){ 
    displayRules(); 
    shuffleCard(cardPile); 

} 

void displayRules(){ 
    cout << "\nWelcome to GoHome! The main objective of this game is to reach Home" 
      " first." << endl; 
    cout << "The basic rules of the game are as follows:" << endl; 
    cout << "\n-To begin the player with the shortest name goes first." << endl; 
    cout << "-Each player picks a card that has a number on it and the player" 
      " must moves forward that many number of spaces." << endl; 
    cout << "-If a card says 'Lose A Turn', the player does nothing and the" 
      "turn moves to the next player." << endl; 
    cout << "-If a card says 'Switch Places', that player is allowed to switch" 
      " places with any player on the board." << endl; 
    cout << "-If a player lands on an obstacle, that player must move back that" 
      " many number of spaces." << endl; 
    cout << "-If a player lands another obstacle while moving backwards, then it" 
      " does not have to move backwards again.\n"<<endl; 
} 

int shuffleCard(int cardPile[]){ 

    srand(time(0)); 
    for(int i = 0; i < 10; i++){ 
      int size = rand() % 10; 
     cout << cardPile[size] << endl; 
    } 
} 

私は、教授が、メインが再生機能を呼びたいだけであり、他のすべては機能で行う必要があると述べている宿題をしています。C++の初心者は、別の関数内で関数を呼び出すことは可能ですか?

基本的に、彼は関数が他の関数を呼び出すことを望んでいました。これまでのところ私は2つの機能を1つのプレイと呼ばれ、もう1つはシャッフルカードと呼ばれています。私の問題は、私がどのようにshuffleCard関数を呼び出すplay関数を取得するか分からないということです。 play関数は問題なくdisplayRules関数を呼び出しますが、コンパイルしようとするとという宣言されていない識別子 'cardPile'を使用しているというエラーが表示されます。

+0

もちろん可能です。あなたはたぶん前方宣言を指定することを忘れましたか? – user0042

+2

C++の本を開き、 "前方宣言"の仕組みを説明する章を読んでください。 –

+2

コンパイラが '宣言されていない識別子の使用 'cardPile'と言うとき、' play'の中では名前(識別子) 'cardPile'を使用していますが、そのような変数は見つかりませんでした。あなたは 'main'の中にあるものを持っていますが、' main'の外側からはアクセスできません。別のローカル変数 'int cardPile [10] = {1,1,2,2,3,3,4,4,0,5};を作るか、' cardPile'を 'play'に渡す必要があります。それを 'shuffleCard'に渡してください。 – nwp

答えて

0
#include <iostream> 
#include <cstdlib> 
#include <cmath> 

using namespace std; 

void displayRules(); 
void play(int cardPile[]); 
int shuffleCard(int cardPile[]); 


int main() 
{ 
    int board[26] = {0, 1, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0}; 
    int cardPile[10] = {1, 1, 2, 2, 3, 3, 4, 4, 0, 5}; 
    int player1 = 0; 
    int player2 = 0; 

    play(cardPile); 


    return 0; 
} 

void play(int cardPile[]){ 
    displayRules(); 
    shuffleCard(cardPile); 

} 

void displayRules(){ 
    cout << "\nWelcome to GoHome! The main objective of this game is to reach Home" 
      " first." << endl; 
    cout << "The basic rules of the game are as follows:" << endl; 
    cout << "\n-To begin the player with the shortest name goes first." << endl; 
    cout << "-Each player picks a card that has a number on it and the player" 
      " must moves forward that many number of spaces." << endl; 
    cout << "-If a card says 'Lose A Turn', the player does nothing and the" 
      "turn moves to the next player." << endl; 
    cout << "-If a card says 'Switch Places', that player is allowed to switch" 
      " places with any player on the board." << endl; 
    cout << "-If a player lands on an obstacle, that player must move back that" 
      " many number of spaces." << endl; 
    cout << "-If a player lands another obstacle while moving backwards, then it" 
      " does not have to move backwards again.\n"<<endl; 
} 

int shuffleCard(int cardPile[]){ 

    srand(time(0)); 
    for(int i = 0; i < 10; i++){ 
      int size = rand() % 10; 
     cout << cardPile[size] << endl; 
    } 
} 
関連する問題