1

初めて投稿しましたが、この課題をほぼ完了することができました。プログラムはエラーを出していませんが、望ましくない結果が得られています。出力は、入力したデータではなく配列アドレスを出力しているようです。または少なくとも、それは私の非常に限られた知識に基づいていると思います. これを修正する方法を教えてくれる人はいますか? 私はこれに一日中取り組んできましたが、この時間にタオルを投げる準備ができていると思います. どんなアドバイスでも大歓迎です、ありがとう!

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

3 に答える 3

2

ここでループを意図しているように見えますが、形式が正しくありません。

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 >> ...行は中括弧の外側で、ステートメントの後に来ます。したがって、反復はステートメントにのみ適用されます。完了すると、制御はに進みますが、x(から残った)はに等しいので、配列の外側、無人地帯にあります。forcin >> ...double max = aPlayer[x].score;for(...)szaPlayer[x]

cin >> ...ブラケットの内側に入れます。

于 2012-10-08T06:32:32.687 に答える
1

問題は、要素のみを印刷することですaPlayer[x]。ただし、最初の for ループはx = 11. まあ、それは配列の最後を1つ過ぎたところにあるので、実際にはジャンクを出力しています.

aPlayer[0]代わりに試してみたところ、期待どおりの結果が得られました。

于 2012-10-08T06:32:41.830 に答える
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;
}
于 2012-10-09T03:01:39.103 に答える