C++数组输出地址
C++ Array outputting address?
我第一次发帖,几乎能够完成这个作业。该程序没有给出错误,但我得到了不希望的结果。输出似乎是输出数组地址,而不是我输入的数据。或者至少我认为这是基于我非常非常有限的知识。任何人都可以帮助指导我如何解决此问题吗?我整天都在为此工作,在这个时候,我想我已经准备好认输了。任何建议都非常感谢,谢谢!
// 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 >> ...
放在括号内。
问题是你只打印元素aPlayer[x]
.但请注意,您的第一个 for 循环在 x = 11
时终止。嗯,这是数组的末尾,所以真的,你在输出垃圾。
我只是用aPlayer[0]
尝试了一下,并得到了预期的结果。
这就是
我最终得到的。感谢大家的帮助!
// 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;
//soccer player class - see above description
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 constructor
SoccerPlayer::SoccerPlayer(int jersey = 0, int goal = 0, int assist = 0)
{
jerseyNum = jersey;
numGoals = goal;
numAssists = assist;
}
//to display star player
void SoccerPlayer::DisplayStar()
{
cout<<"Jersey #: "<< jerseyNum <<endl;
cout<<"Goals Scored: "<< numGoals <<endl;
cout<<"Assists Made: "<< numAssists <<endl;
}
//overload operator out
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;
}
//overload operator in
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;
}
//overload operator greater than
int SoccerPlayer::operator>(SoccerPlayer& aPlayer)
{
int total = 0;
if (score > aPlayer.score)
total = 1;
return total;
}
//main declaration
int main()
{
//11 players
const int sz = 11;
int x;
SoccerPlayer aPlayer[sz];
double max = 0;
//to display star
SoccerPlayer Star;
//allow user to input players, show what they input
for(x = 0; x < sz; ++x)
{
cin >> aPlayer[x];
cout << aPlayer[x] << endl << endl;
for(int i=1; i<sz; i++)
{
Star=aPlayer[0];
if(aPlayer[i] > aPlayer[i+1])
Star=aPlayer[i];
}
}
//show star player
cout << "The Star Player: "<< endl;
Star.DisplayStar();
_getch();
return 0;
}
相关文章:
- 将数组的地址分配给变量并删除
- 空基优化子对象的地址
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 如何在c++程序中找到函数的地址
- 向量元素的引用地址与它所指向的向量元素的地址不同.为什么
- 被解释为低级别const的const对象的地址
- 将地址分配给本地指针后,公共对象的变量将消失
- 为什么我在leetcode上收到AddressSanitizer:地址0x602000000058上的堆缓冲区溢出错误
- 内联程序集printf将整数解释为地址
- 为什么指针不写入类的地址?
- 如何在C++中获取该对象的类声明中对象的地址?
- 通过按地址访问变量
- 当一个新对象被分配到它的地址时,对象是否必须被销毁
- 函数名是c中该函数的第一条指令的地址吗
- Visual Studio(或任何其他工具)能否将地址解释为调用堆栈(boost上下文)的开头
- ReadProcessMemory() 不适用于像 0x2840C6C68D8 这样的长地址
- CUDA:统一内存和指针地址的更改
- 当我们从/tp地址中添加/减去一个整数时会发生什么
- 如何使用 Boost Asio 在 Android 上获取我的本地 udp IP 地址?