2016-06-15 9 views
0

私はC++で完了しなければならない課題について簡単に質問します。先生は、私は下の機能が含まれていることを必要としていますポインタを使わずにC++で配列を参照として渡す

void getPlayerInfo(Player &); 
void showInfo(Player); 
int getTotalPoints(Player [], int); 

しかし、私は最初の関数に取り組んでトラブルを抱えている....私は、構造体の配列を適切に呼んでいるかはわかりません。誰かがそれを見て、私が間違っていることを見ることができますか?私はそれをちょっと試して、配列を呼び出して配列へのポインタを渡すことができましたが、先生は "&"というシンボルがあることを要求しました。私は認識していない別の方法が必要です。助けてください!おかげ

#include <iostream> 
#include <iomanip> 
#include <string> 
using namespace std; 

// Structure to hold information about a player 
struct Player 
{ 
    string name;  // to hold the players name 
    int number;   // to hold players number 
    int points;   // to hold the points scored by the player 
}; 

// Function prototypes 
void getPlayerInfo(Player &); // function to get the players information  from the user 
void showInfo(Player); // function to show the table 

int main() 
{ 
    const int numPlayers = 12; // Constant to hold the number of players 
    Player team[numPlayers]; // array to hold 12 structures 

          // Gather information about all 12 players 
    getPlayerInfo(team); 

    showInfo(team); 


    return 0; 
} 

// Function to get the players info 
void getPlayerInfo(Player& team) 
{ 
    for (int count = 0; count < 12; count++) 
{ 
    cout << "PLAYER #" << (count + 1) << endl; 
    cout << "----------" << endl; 
    cout << "Player name: "; 
    cin.ignore(); 
    getline(cin, team[count].name); 
    cout << "Player's number: "; 
    cin >> team[count].number; 
    cout << "Points scored: "; 
    cin >> team[count].points; 
    cout << endl; 
} 

}

getPlayerInfo()
+0

あなたの教授が教えてくれたはずのリャクチュアを聞いたことがありますか?あなたがそうしなかったように見えるので、今ではC++の本を読む必要があります。 – SergeyA

+0

@ SergeyAおそらく、あなたはそれらの教授を過大評価するでしょう。それとも、それは私だけの悪い経験をすべて持っている... – DeiDei

答えて

4

アレイを受け入れていない、それは単一Playerオブジェクトへの参照を受け付けます。

アレイ内の各プレイヤーにはgetPlayerInfo()と電話する必要があります。ループをgetPlayerInfo()の外に移動してmain()に移動します。

1

あなたはこれらの機能の意図を誤解しています。ご提供いただいた情報から推測

は、getPlayerInfo個々プレイヤーの情報を取得することを目的とし、showPlayerInfo個々プレイヤーの情報を示すことを意図しています。

これらの機能を使用して、意図しない機能を実行しようとしています。そのため、呼び出す方法や実装方法がわからないことがあります。

この経験は、要件収集のオブジェクトレッスンとして考えてください。

関連する問題