2014-12-02 3 views
0

나는 매 쿼터와 메인 게임에서 승자를 결정할 수있는 농구 스코어 보드를 만들고 있습니다. 배열에 내 변수 값을 저장할 수 있습니까? "Q1teamOne, Q2teamOne, Q3teamOne, Q4teamOne"의 값을 배열에 넣고 "Q1teamTwo, Q2teamTwo, Q3teamTwo, Q4teamTwo"의 값을 넣거나 배열의 요소로 만들고 싶습니다. 여기 배열에 변수를 넣는 방법

#include <functional> 

//... 


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

std::reference_wrapper<int> teamTwo[] = { 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;'? 그리고 당신은 이미 스코어에 대한 배열을 가지고 있습니다. 즉, 배열을 만드는 방법을 이미 알고 있다는 것을 의미합니다. 그래서 당신의 문제는 무엇입니까? –

+0

그래, 배열을 만드는 방법을 알고 있지만 "teamOneScore"배열에 변수 값을 어떻게 두어야합니까? –

+0

배열 대신'vector'를 사용해야합니다. 컬렉션을 저장하는 C++ 방식입니다. –

답변

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 이중의 배열이며, 점수가 너무, INT 있습니다

  • 변화 배열의 유형의 int로 또는
  • 가 할당 캐스팅 as : teamOneScore [0] = static_cast < double> (Q1teamOne); 또한

은, 당신의 정보는,이 비교가 올바르지 않습니다 :

다른

경우 그것은해야한다 (Q4TeamOneTotal = Q4TeamTwoTotal)

:

다른

경우 (Q4TeamOneTotal == Q4TeamTwoTotal)

마지막으로 배열을 사용하여 cin의 점수를 저장하고 Q1teamOne, ... vars의 사용을 피할 수 있습니다.

+0

나는 당신의 충고에 따라 도움을 주셔서 감사합니다. –

3

은 프로그램 출력된다

#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 (int x : { Q1teamOne, Q2teamOne, Q3teamOne, Q4teamOne }) std::cout << x << ' '; 
std::cout << std::endl; 
관련 문제