2017-09-24 99 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; 
    } 
} 

我正在做一个家庭作业,其中我的教授特别提到他只想要主要调用play函数,而其他所有事情都应该在函数中完成。C++初学者可以在另一个函数中调用函数吗?

基本上他想要函数调用其他函数。到目前为止,我有两个函数,一个叫做play,另一个叫做shuffleCard。我的问题是我不确定如何让播放函数调用shuffleCard函数。 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; 
    } 
} 
相关问题