gpt4 book ai didi

c++ - 在不使用指针的情况下在 C++ 中将数组作为引用传递

转载 作者:太空宇宙 更新时间:2023-11-04 16:04:34 25 4
gpt4 key购买 nike

我有一个关于我必须完成的 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() 不接受数组,它接受对单个 Player 对象的引用。

您需要为数组中的每个玩家调用 getPlayerInfo()。将循环从 getPlayerInfo() 移到 main() 中。

关于c++ - 在不使用指针的情况下在 C++ 中将数组作为引用传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37837614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com