gpt4 book ai didi

C++数组输出地址?

转载 作者:行者123 更新时间:2023-11-30 02:59:37 31 4
gpt4 key购买 nike

我第一次发帖较早,几乎可以完成这项作业。该程序没有给出错误,但我得到了不希望的结果。输出似乎是输出数组地址而不是我输入的数据。或者至少我认为这是基于我非常非常有限的知识。谁能帮助指导我如何解决这个问题?我整天都在为此工作,在这个时候,我想我已经准备好认输了。非常感谢任何建议,谢谢!

// Amanda 
// SoccerPlayer.cpp : main project file.
// October 6, 2012
/* a. Design a SoccerPlayer class that includes three integer fields: a player's jersey number,
number of goals, and number of assists. Overload extraction and insertion operators for the class.
b. Include an operation>() function for the class. One SoccerPlayer is considered greater
than another if the sum of goals plus assists is greater.
c. Create an array of 11 SoccerPlayers, then use the > operator to find the player who has the
greatest goals plus assists.*/

#include "stdafx.h"
#include<conio.h>
#include<iostream>
#include<string>
using namespace std;



class SoccerPlayer
{
friend std::ostream& operator<<(std::ostream&, const SoccerPlayer&);
friend istream& operator>>(istream&, SoccerPlayer&);
private:
int jerseyNum;
int numGoals;
int numAssists;
public:
SoccerPlayer(int, int, int);
int score;
int operator>(SoccerPlayer&);
void DisplayStar();

};

SoccerPlayer::SoccerPlayer(int jersey = 0, int goal = 0, int assist = 0)
{
jerseyNum = jersey;
numGoals = goal;
numAssists = assist;
}

void SoccerPlayer::DisplayStar()
{
cout<<"Player Number: "<< jerseyNum <<endl;
cout<<"Goals Scored: "<< numGoals <<endl;
cout<<"Assists Made: "<< numAssists <<endl;
}

std::ostream& operator<<(std::ostream& player, const SoccerPlayer& aPlayer)
{
player << "Jersey #" << aPlayer.jerseyNum <<
" Number of Goals " << aPlayer.numGoals <<
" Number of Assists " << aPlayer.numAssists;
return player;
}

std::istream& operator>>(std::istream& inPlayer, SoccerPlayer& aPlayer)
{

cout << "Please enter the jersey number: ";
inPlayer >> aPlayer.jerseyNum;
cout << "Please enter the number of goals: ";
inPlayer >> aPlayer.numGoals;
cout << "Please enter the number of assists: ";
inPlayer >> aPlayer.numAssists;

aPlayer.score=(aPlayer.numGoals) + (aPlayer.numAssists);



return inPlayer;
}

int SoccerPlayer::operator>(SoccerPlayer& aPlayer)
{

int total = 0;
if (score > aPlayer.score)
total = 1;
return total;


}

int main()
{
const int sz = 11;
int x;

SoccerPlayer aPlayer[sz];

for(x = 0; x < sz; ++x)
cin >> aPlayer[x];

{
double max = aPlayer[x].score;
for(int i = 1; i<sz; ++i)
{
if(aPlayer[i] > aPlayer[x])
{
max=aPlayer[i].score;
}

}
cout << max << endl;
cout << aPlayer[x];
}
_getch();
return 0;
}

最佳答案

看起来好像你打算在这里循环,但它的格式不正确:

for(x = 0; x < sz; ++x)
cin >> aPlayer[x];

{
double max = aPlayer[x].score;
for(int i = 1; i<sz; ++i)
{
if(aPlayer[i] > aPlayer[x])
{
max=aPlayer[i].score;
}

}
cout << max << endl;
cout << aPlayer[x];
}

你的意思是(我认为)double max ... 应该在循环内,但是 cin >> ... 行在之后 for 语句, 大括号之外。所以迭代只适用于 cin >> ... 语句;完成后,控制将转到 double max = aPlayer[x].score;,但是 x(for(...) 遗留下来的)等于 sz,所以 aPlayer[x] 在数组之外,在无人区。

cin >> ... 放在括号内。

关于C++数组输出地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12776221/

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