0

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

}

2
  • Did you even listen to the leacture your professor should've given you? Looks like you did not, so now you need to read a book on C++. Commented Jun 15, 2016 at 14:02
  • @SergeyA Perhaps you overestimate them professors. Or maybe it's just me with all the bad experience... Commented Jun 15, 2016 at 14:14

2 Answers 2

4

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().

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.