如何将变量放入数组中

时间:2014-12-02 08:31:39

标签: c++ arrays variables elements

我正在制作一个篮球记分牌,可以确定每个季度和主要游戏的赢家。 如何将变量的值存储在数组中? 我想把" 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";
    }

2 个答案:

答案 0 :(得分: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 ( int x : { Q1teamOne, Q2teamOne, Q3teamOne, Q4teamOne } ) std::cout << x << ' ';
std::cout << std::endl;

答案 1 :(得分: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是double的数组,你的得分是int,所以:

  • 将数组的类型更改为整数或
  • 将作业转换为:teamOneScore [0] = static_cast&lt; double&gt;(Q1teamOne);

此外,仅供参考,此比较不正确:

否则if(Q4TeamOneTotal = Q4TeamTwoTotal)

应该是:

否则if(Q4TeamOneTotal == Q4TeamTwoTotal)

作为最后一点,您可以使用数组来存储cin中的分数,并避免使用Q1teamOne,... vars。