【问题标题】:C++ Array outputting address?C ++数组输出地址?
【发布时间】:2012-10-08 06:16:28
【问题描述】:

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

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

【问题讨论】:

  • 目前我唯一要提供的是,您应该尽可能多地删除不相关的代码。
  • 测试一下,它似乎是大数字,而不是地址。
  • 如果 x 旨在表示第二个 for 循环中最高得分玩家的索引,则应在循环开始之前将其初始化为零并在 'if(player[i] > 中更新。 ..)' 块。

标签: c++ arrays operator-overloading


【解决方案1】:

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

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 &gt;&gt; ... 行在 for 语句之后,在大括号之外 .所以迭代只适用于cin &gt;&gt; ... 语句;完成后,控制权转到double max = aPlayer[x].score;,但 x(从for(...) 剩下的)等于sz,所以aPlayer[x] 在数组之外,在无人区。

将cin &gt;&gt; ... 放在括号内。

【讨论】:

    【解决方案2】:

    问题是你只打印元素aPlayer[x]。但请注意,您的第一个 for 循环在 x = 11 时终止。嗯,这是数组末尾的一个,所以说真的,你输出的是垃圾。

    我只是用aPlayer[0] 试了一下,得到了预期的结果。

    【讨论】:

      【解决方案3】:

      这就是我最终的结果。感谢大家的帮助!

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-09-08
        • 1970-01-01
        • 2013-01-23
        • 1970-01-01
        • 2014-05-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多