C ++初学者可以在另一个函数中调用函数吗?

时间:2017-09-24 19:37:53

标签: c++ macos

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

我做了一个家庭作业,我的教授特别提到他只想要主要调用播放功能,其他一切都应该在功能中完成。

基本上他希望函数调用其他函数。到目前为止,我有两个函数,一个叫做play,另一个叫做shuffleCard。我的问题是,我不知道如何让play函数调用shuffleCard函数。 play函数调用displayRules函数没有任何问题,但是当我尝试编译它时,我收到一个错误,说明使用未声明的标识符&#39; cardPile&#39;。

1 个答案:

答案 0 :(得分: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;
    }
}