2014-12-02 106 views
0

我製作的籃球記分牌可以確定每個季度的遊戲勝者和主遊戲。 如何將我的變量的值存儲在數組中? 我想將「Q1teamOne,Q2teamOne,Q3teamOne,Q4teamOne」的值放在一個數組中,並且還要將「Q1teamTwo,Q2teamTwo,Q3teamTwo,Q4teamTwo」的值或它們作爲數組的元素。如何將變量放入數組

#include <iostream> 
#include <string> 
using namespace std; 

int main() 
{ 
string Team1; 
string Team2; 
double OTscore1; 
double OTscore2; 
int Q1teamOne, Q2teamOne, Q3teamOne, Q4teamOne; 
int Q1teamTwo, Q2teamTwo, Q3teamTwo ,Q4teamTwo; 
int Q2TeamOneTotal, Q3TeamOneTotal, Q4TeamOneTotal; 
int Q2TeamTwoTotal, Q3TeamTwoTotal, Q4TeamTwoTotal; 
double teamOneScore[4]; 
double teamTwoScore[4]; 
int index; 
double sumOne, sumTwo; 

cout << "BASKETBALL SCOREBOARD:\n" << endl; 
cout << "Enter Team 1 name: "; 
getline (cin, Team1); 
cout << "Enter Team 2 name: "; 
getline (cin, Team2); 

//FIRST QUARTER 

cout << "\nQUARTER 1:\n\n"; 
cout << "Team " << Team1 << " Score: "; 
cin >> Q1teamOne; 

cout << "Team " << Team2 << " Score: "; 
cin >> Q1teamTwo; 

if (Q1teamOne > Q1teamTwo) 
    { 
     cout <<"****" << "Team " << Team1 << " is Leading.****\n\n"; 
    } 
else if (Q1teamOne < Q1teamTwo) 
    { 
     cout <<"****" << Team2 << " is Leading.****\n\n"; 
    } 
else if (Q1teamOne = Q1teamTwo) 
{ 
    cout <<"****We Have a Tie!!****\n\n"; 
} 


//SECOND QUARTER 
cout << "\nQUARTER 2:\n\n"; 
cout << "Team " << Team1 << " Score: "; 
cin >> Q2teamOne; 
Q2TeamOneTotal = Q1teamOne + Q2teamOne; 
cout <<"Total Score: "<< Q2TeamOneTotal <<endl;; 

cout << "Team " << Team2 << " Score: "; 
cin >> Q2teamTwo; 
Q2TeamTwoTotal = Q1teamTwo + Q2teamTwo; 
cout <<"Total Score: " << Q2TeamTwoTotal; 


if (Q2TeamOneTotal > Q2TeamTwoTotal) 
    { 
     cout <<"\n****" << Team1 << " is Leading.****\n\n"; 
    } 
else if (Q2TeamOneTotal < Q2TeamTwoTotal) 
    { 
     cout <<"\n****" << Team2 << " is Leading.****\n\n"; 
    } 
else if (Q2TeamOneTotal = Q2TeamTwoTotal) 
{ 
    cout <<"\n****We Have a Tie!!****\n\n"; 
} 


//THIRD QUARTER 
cout << "\nQUARTER 3:\n\n"; 
cout << "Team " << Team1 << " Score: "; 
cin >> Q3teamOne; 
Q3TeamOneTotal = Q1teamOne + Q2teamOne + Q3teamOne; 
cout <<"Total Score: "<< Q3TeamOneTotal <<endl;; 

cout << "Team " << Team2 << " Score: "; 
cin >> Q3teamTwo; 
Q3TeamTwoTotal = Q1teamTwo + Q2teamTwo + Q3teamTwo; 
cout <<"Total Score: " << Q3TeamTwoTotal; 


if (Q3TeamOneTotal > Q3TeamTwoTotal) 
    { 
     cout <<"\n****" << Team1 << " is Leading.****\n\n"; 
    } 
else if (Q3TeamOneTotal < Q3TeamTwoTotal) 
    { 
     cout <<"\n****" << Team2 << " is Leading.****\n\n"; 
    } 
else if (Q3TeamOneTotal = Q3TeamTwoTotal) 
    { 
    cout <<"\n****We Have a Tie!!****\n\n"; 
    } 

//FOURTH QUARTER 

cout << "\nQUARTER 4:\n\n"; 
cout << "Team " << Team1 << " Score: "; 
cin >> Q4teamOne; 
Q4TeamOneTotal = Q1teamOne + Q2teamOne + Q3teamOne + Q4teamOne; 
cout <<"Total Score: "<< Q4TeamOneTotal <<endl; 

cout << "Team " << Team2 << " Score: "; 
cin >> Q4teamTwo; 
Q4TeamTwoTotal = Q1teamTwo + Q2teamTwo + Q3teamTwo + Q4teamTwo; 
cout <<"Total Score: " << Q4TeamTwoTotal; 


    if (Q4TeamOneTotal > Q4TeamTwoTotal) 
    { 
     cout <<"\n****" << Team1 << " is Leading.****\n\n"; 
    } 
else if (Q4TeamOneTotal < Q4TeamTwoTotal) 
    { 
     cout <<"\n****" << Team2 << " is Leading.****\n\n"; 
    } 
else if (Q4TeamOneTotal = Q4TeamTwoTotal) 
    { 
    cout <<"\n****We Have a Tie!!****\n\n"; 
    } 
+1

'std :: array teamOneArray;'?你已經擁有了數組的分數,這意味着你已經知道如何創建數組了,那麼你的問題是什麼? – 2014-12-02 08:34:06

+0

是的我知道如何創建數組,但是我如何將變量值放在「teamOneScore」數組中? – 2014-12-02 08:56:53

+0

你應該使用'vector'而不是數組。這是存儲集合的C++方式。 – 2014-12-02 09:21:18

回答

1

什麼約:

teamOneScore [0] = Q1teamOne;

teamOneScore [1] = Q2teamOne;

teamOneScore [2] = Q3teamOne;

teamOneScore [3] = Q4teamOne;

teamTwoScore [0] = Q1teamTwo;

teamTwoScore [1] = Q2teamTwo;

teamTwoScore [2] = Q3teamTwo;

teamTwoScore [3] = Q4teamTwo;

但是考慮:

陣列teamOneScore和teamTwoScore是雙陣列和你的分數是整型,所以:

  • 變化的數組類型是整數或
  • 投轉讓as:teamOneScore [0] = static_cast < double>(Q1teamOne);

而且,只爲您的信息,這種比較是不正確的:

否則,如果(Q4TeamOneTotal = Q4TeamTwoTotal)

它應該是:如果

其他(Q4TeamOneTotal == Q4TeamTwoTotal)

作爲最後一點,您可以使用數組來存儲cin的分數,並避免使用Q1teamOne,... vars。

+0

我遵循你的建議,並使用cin存儲我的價值觀,感謝您的幫助! – 2014-12-02 10:05:12

3

例如

#include <functional> 

//... 


std::reference_wrapper<int> teamOne[] = { Q1teamOne, Q2teamOne, Q3teamOne, Q4teamOne }; 

std::reference_wrapper<int> teamTwo[] = { Q1teamTwo, Q2teamTwo, Q3teamTwo ,Q4teamTwo }; 

下面是一個示範程序

#include <iostream> 
#include <functional> 

int main() 
{ 
    int Q1teamOne, Q2teamOne, Q3teamOne, Q4teamOne; 
    std::reference_wrapper<int> teamOne[] = 
    { 
     Q1teamOne, Q2teamOne, Q3teamOne, Q4teamOne 
    }; 

    int i = 0; 
    for (auto &x : teamOne) x.get() = i++; 

    for (const auto &x : teamOne) std::cout << x << ' '; 
    std::cout << std::endl; 

    return 0; 
} 

程序輸出是

0 1 2 3 

或者,如果原始值和所述陣列之間的鏈路不需要那麼喲你可以簡單地寫

double teamOneScore[] = { Q1teamOne, Q2teamOne, Q3teamOne, Q4teamOne }; 

你也可以在基於for語句的範圍中使用初始值設定項列表而不聲明任何數組。例如

for (int x : { Q1teamOne, Q2teamOne, Q3teamOne, Q4teamOne }) std::cout << x << ' '; 
std::cout << std::endl;