0

I am making a Basketball Scoreboard that can determine the winner of the game in each quarter and the the main game. How can i store the values of my variables in an array? I want to put the values of "Q1teamOne, Q2teamOne, Q3teamOne, Q4teamOne" in an array and also the values of "Q1teamTwo, Q2teamTwo, Q3teamTwo ,Q4teamTwo" or make them elements of an array.

#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";
    }
3
  • 1
    How about std::array<int, 4> teamOneArray;? And you already have arrays for the score, meaning you already know how to make arrays, so what is your problem? Commented Dec 2, 2014 at 8:34
  • yeah i know how to make arrays, but how do i put my variable values in the "teamOneScore" array? Commented Dec 2, 2014 at 8:56
  • You should be using vector instead of arrays. That is the C++ way to store collections. Commented Dec 2, 2014 at 9:21

2 Answers 2

3

For example

#include <functional>

//...


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

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

Here is a demonstrative program

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

The program output is

0 1 2 3

Or if the link between the original values and the array is not need then you could write simply

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

Also you could use initializer list in the range based for statement without declaring any array. For example

for ( int x : { Q1teamOne, Q2teamOne, Q3teamOne, Q4teamOne } ) std::cout << x << ' ';
std::cout << std::endl;
Sign up to request clarification or add additional context in comments.

Comments

1

what about:

teamOneScore[ 0 ] = Q1teamOne;

teamOneScore[ 1 ] = Q2teamOne;

teamOneScore[ 2 ] = Q3teamOne;

teamOneScore[ 3 ] = Q4teamOne;

teamTwoScore[ 0 ] = Q1teamTwo;

teamTwoScore[ 1 ] = Q2teamTwo;

teamTwoScore[ 2 ] = Q3teamTwo;

teamTwoScore[ 3 ] = Q4teamTwo;

But consider:

the arrays teamOneScore and teamTwoScore are arrays of double and your scores are int, so:

  • change the type of the arrays to be ints or
  • cast the assignment as: teamOneScore[ 0 ] = static_cast< double >( Q1teamOne );

Also, just for your information, this comparison is not correct:

else if (Q4TeamOneTotal = Q4TeamTwoTotal)

It should be:

else if (Q4TeamOneTotal == Q4TeamTwoTotal)

As a final note, you can use the arrays to store the scores from the cin and avoid the use of the Q1teamOne, ... vars.

1 Comment

I followed your advice and used the cin to store my values, thanks for the help!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.