c++ - How to put variables into an array -
i making basketball scoreboard can determine winner of game in each quarter , the main game. how can store values of variables in array? want put values of "q1teamone, q2teamone, q3teamone, q4teamone" in array , values of "q1teamtwo, q2teamtwo, q3teamtwo ,q4teamtwo" or make them elements of 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 << " leading.****\n\n"; } else if (q1teamone < q1teamtwo) { cout <<"****" << team2 << " leading.****\n\n"; } else if (q1teamone = q1teamtwo) { cout <<"****we have 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 << " leading.****\n\n"; } else if (q2teamonetotal < q2teamtwototal) { cout <<"\n****" << team2 << " leading.****\n\n"; } else if (q2teamonetotal = q2teamtwototal) { cout <<"\n****we have 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 << " leading.****\n\n"; } else if (q3teamonetotal < q3teamtwototal) { cout <<"\n****" << team2 << " leading.****\n\n"; } else if (q3teamonetotal = q3teamtwototal) { cout <<"\n****we have 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 << " leading.****\n\n"; } else if (q4teamonetotal < q4teamtwototal) { cout <<"\n****" << team2 << " leading.****\n\n"; } else if (q4teamonetotal = q4teamtwototal) { cout <<"\n****we have tie!!****\n\n"; }
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 , teamtwoscore arrays of double , scores int, so:
- change type of arrays ints or
- cast assignment as: teamonescore[ 0 ] = static_cast< double >( q1teamone );
also, information, comparison not correct:
else if (q4teamonetotal = q4teamtwototal)
it should be:
else if (q4teamonetotal == q4teamtwototal)
as final note, can use arrays store scores cin , avoid use of q1teamone, ... vars.
Comments
Post a Comment