Jambvant Ramoutar
Jambvant Ramoutar

Reputation: 3

Passing array off as reference in c++ without using pointer

I have a quick question regarding an assignment I have to complete for C++. The teacher has required that I include the functions below:

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

But I'm having trouble working on the first function....I'm not sure if I'm calling the array of structures properly. Can someone look it over and see what I'm doing wrong? I fiddled with it a bit and I'm able to call the array and pass off a pointer to the array but the teacher requested the "&" symbol be there so there must be another method I'm unaware of. Please help! Thanks

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

}

Upvotes: 0

Views: 87

Answers (2)

user1084944
user1084944

Reputation:

You've misunderstood the intent of these functions.

Guessing from the information you've provided, getPlayerInfo is intended to get the information of an individual player, and showPlayerInfo is intended to show the information of an individual player.

You're trying to use these functions to do something they aren't intended to do, so it's a good thing that you are having trouble figuring out how to call and how to implement them.

Consider this experience as a object lesson in requirement gathering.

Upvotes: 1

Karl Nicoll
Karl Nicoll

Reputation: 16419

getPlayerInfo() does not accept an array, it accepts a reference to a single Player object.

You need to call getPlayerInfo() for each player in your array. Move your loop outside of getPlayerInfo() and into main().

Upvotes: 4

Related Questions