2012-10-08 43 views
1

我之前首次發佈,幾乎可以完成這項任務。該程序沒有給出錯誤,但我得到了不希望的結果。輸出看起來是輸出陣列地址而不是輸入數據。或者至少我認爲這是基於我非常有限的知識。任何人都可以幫助指導我如何解決這個問題?我一整天都在努力,在這個時刻,我想我已經準備好了。任何意見非常感謝,謝謝!C++數組輸出地址?

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

我現在唯一要提供的是,你應該儘可能地消除不相關的代碼。 – chris

+1

測試一下,它看起來很大,而不是地址。 – chris

+1

如果x用於表示第二個for循環中最大得分玩家的指數,它應該在循環開始之前初始化爲零,並在'if(player [i]> ...)'區塊內更新。 –

回答

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 >> ...全系標配for聲明,外部大括號。因此迭代僅適用於cin >> ...聲明;一旦完成,控制進行到double max = aPlayer[x].score;,但x(從for(...)剩餘)等於sz,因此aPlayer[x]位於陣列之外,無人地帶。

cin >> ...放在括號內。

1

問題是你只打印元素aPlayer[x]。但請注意,您的第一個for循環在x = 11時終止。那麼,這是通過陣列的末尾,真的,你輸出的垃圾。

我只是試了一下aPlayer[0]而不是預期的結果。

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