Marco Lopez Marco Lopez - 24 days ago 9
C++ Question

How to put variables into an array

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

Answer

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.

Comments