Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何将变量放入数组中_C++_Arrays_Variables_Elements - Fatal编程技术网

C++ 如何将变量放入数组中

C++ 如何将变量放入数组中,c++,arrays,variables,elements,C++,Arrays,Variables,Elements,我正在制作一个篮球记分板,它可以决定每季比赛的胜利者和主要比赛的胜负。 如何将变量的值存储在数组中? 我想把“Q1teamOne、Q2teamOne、Q3teamOne、Q4teamOne”的值放在一个数组中,也可以把“Q1teamTwo、Q2teamTwo、Q3teamTwo、Q4teamTwo”的值放在一个数组中,或者将它们作为数组的元素 #include <iostream> #include <string> using namespace std; int m

我正在制作一个篮球记分板,它可以决定每季比赛的胜利者和主要比赛的胜负。 如何将变量的值存储在数组中? 我想把“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";
    }
#包括
#包括
使用名称空间std;
int main()
{
字符串组1;
字符串组2;
双OT1;
双OT2;
int Q1teamOne、Q2teamOne、Q3teamOne、Q4teamOne;
国际Q1teamTwo、Q2teamTwo、Q3teamTwo、Q4teamTwo;
int Q2TeamOneTotal、Q3TeamOneTotal、Q4TeamOneTotal;
int Q2TeamTwoTotal、Q3TeamTwoTotal、Q4TeamTwoTotal;
双teamOneScore[4];
两队两分[4];
整数指数;
双相扑一号,相扑二号;
例如cout

#include <functional>

//...


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

std::reference_wrapper<int> teamTwo[] = { Q1teamTwo, Q2teamTwo, Q3teamTwo ,Q4teamTwo };
for ( int x : { Q1teamOne, Q2teamOne, Q3teamOne, Q4teamOne } ) std::cout << x << ' ';
std::cout << std::endl;
或者,如果不需要原始值和数组之间的链接,则可以简单地编写

double teamOneScore[] = { Q1teamOne, Q2teamOne, Q3teamOne, Q4teamOne };
您还可以在基于范围的for语句中使用初始值设定项列表,而不声明任何数组

#include <functional>

//...


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

std::reference_wrapper<int> teamTwo[] = { Q1teamTwo, Q2teamTwo, Q3teamTwo ,Q4teamTwo };
for ( int x : { Q1teamOne, Q2teamOne, Q3teamOne, Q4teamOne } ) std::cout << x << ' ';
std::cout << std::endl;
for(intx:{Q1teamOne,Q2teamOne,Q3teamOne,Q4teamOne})std::cout例如

#include <functional>

//...


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

std::reference_wrapper<int> teamTwo[] = { Q1teamTwo, Q2teamTwo, Q3teamTwo ,Q4teamTwo };
for ( int x : { Q1teamOne, Q2teamOne, Q3teamOne, Q4teamOne } ) std::cout << x << ' ';
std::cout << std::endl;
或者,如果不需要原始值和数组之间的链接,则可以简单地编写

double teamOneScore[] = { Q1teamOne, Q2teamOne, Q3teamOne, Q4teamOne };
您还可以在基于范围的for语句中使用初始值设定项列表,而不声明任何数组

#include <functional>

//...


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

std::reference_wrapper<int> teamTwo[] = { Q1teamTwo, Q2teamTwo, Q3teamTwo ,Q4teamTwo };
for ( int x : { Q1teamOne, Q2teamOne, Q3teamOne, Q4teamOne } ) std::cout << x << ' ';
std::cout << std::endl;
for(intx:{Q1teamOne,Q2teamOne,Q3teamOne,Q4teamOne})std::cout

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,因此:

  • 将数组的类型更改为int或
  • 将作业转换为:teamOneScore[0]=静态_cast(Q1teamOne)
此外,仅供参考,此比较不正确:

否则如果(Q4TeamOneTotal=Q4TeamTwoTotal)

应该是:

否则如果(Q4TeamOneTotal==Q4TeamTwoTotal)

最后,您可以使用数组存储cin的分数,避免使用Q1teamOne,…VAR。

关于:

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,因此:

  • 将数组的类型更改为int或
  • 将作业转换为:teamOneScore[0]=静态_cast(Q1teamOne)
此外,仅供参考,此比较不正确:

否则如果(Q4TeamOneTotal=Q4TeamTwoTotal)

应该是:

否则如果(Q4TeamOneTotal==Q4TeamTwoTotal)


最后,您可以使用数组存储cin中的分数,避免使用Q1teamOne,…VAR。

关于
std::array teamOneArray;
?您已经有了分数数组,这意味着您已经知道如何创建数组,那么您的问题是什么呢?是的,我知道如何创建数组,但如何放置变量va“teamOneScore”中的lues数组?你应该使用“代码>向量<代码>代替数组。这是C++存储集合的方法。<代码> STD::数组TeaMeNeRay.<代码>,并且你已经有了数组的分数,意思是你已经知道如何制作数组,那么你的问题是什么?是的,我知道如何制作数组,但是我如何把变量值放在数组中呢?“TeaveSnCype”数组?你应该使用“代码>向量<代码>代替数组。这是存储集合的C++方式。我遵循你的建议,并使用CIN存储我的值,谢谢帮助!我遵照你的建议,并用CIN存储我的值,谢谢帮助!!