Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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+中的两个不同平均值+;_C++_Average_Dev C++ - Fatal编程技术网

C++ 计算C+中的两个不同平均值+;

C++ 计算C+中的两个不同平均值+;,c++,average,dev-c++,C++,Average,Dev C++,我的任务是计算保龄球的平均成绩。我有五名球员,每个球员三场比赛。我现在有两个循环在运行,一个用于玩家,另一个用于游戏编号。我需要显示每个循环结束时球员的平均值,以及团队在该循环结束时的平均值 我修复了我的代码,并用下面的新代码替换了我的旧代码。在我检查这里查看每个人的评论等之前,我一直在玩它,到那时我已经解决了它 但是谢谢大家 #include <iostream> using namespace std; int main() { //DECLARATIONS const in

我的任务是计算保龄球的平均成绩。我有五名球员,每个球员三场比赛。我现在有两个循环在运行,一个用于玩家,另一个用于游戏编号。我需要显示每个循环结束时球员的平均值,以及团队在该循环结束时的平均值

我修复了我的代码,并用下面的新代码替换了我的旧代码。在我检查这里查看每个人的评论等之前,我一直在玩它,到那时我已经解决了它

但是谢谢大家

#include <iostream>

using namespace std;

int main()
{
//DECLARATIONS
const int PLAYER_NUMBER = 5; //There are five players total
const int GAME_NUMBER = 3; //There are three games total
const int MIN = 0; //Min number
const int MAX = 300; //Max number
double* playerScore; //The players' score of current game
double playerAverage = 0; //The current players' average
double teamAverage = 0; //The teams' average

//INPUT

for (int currentPlayer = 0; currentPlayer < PLAYER_NUMBER; currentPlayer++)
{//Set the current player number  

    for (int currentGame = 0; currentGame < GAME_NUMBER; currentGame++)
    {//Set the current game number
             //Get scores

             cout << "For Player " << (currentPlayer + 1) << ", enter score for game " << (currentGame + 1) << ": ";
             cin  >> playerScore[currentGame];


             if(playerScore[currentGame] < MIN || playerScore[currentGame] > MAX)
             {//Check range
                   cout << "The score must be between 0 and 300!\n";
                   currentGame--; //If there is an error, subtract the game number by one
             }//End If statement

             playerAverage += playerScore[currentGame];

             if(currentGame == 2)
             {//Current player average
                cout << endl << "The average for player " << (currentPlayer + 1) << " is: " << (playerAverage / 3) << endl << endl;
                teamAverage += playerAverage;
                playerAverage = 0;
             }//End If statement

    }//End game for-statement

}//End player for-statement

    cout << endl << "The average for the team is: " << (teamAverage / 15) << endl << endl;

//ENDING    
system("Pause");
return 0;    
}//Close main
#包括
使用名称空间std;
int main()
{
//声明
const int PLAYER_NUMBER=5;//总共有五名玩家
const int GAME_NUMBER=3;//总共有三个游戏
常量int MIN=0;//最小值
常量int MAX=300;//最大数量
double*playerScore;//当前游戏的玩家分数
double playerAverage=0;//当前玩家的平均值
double teamAverage=0;//团队的平均值
//输入
对于(int currentPlayer=0;currentPlayer不能您正在声明
double*playerScore
,但我看不出您在哪里分配存储。可能您正在覆盖某些内容

int main()
{
/* ... */
double* playerScore; //The players' score of current game

for (int currentPlayer = 0; currentPlayer < PLAYER_NUMBER; currentPlayer++) {
    for (int currentGame = 0; currentGame < GAME_NUMBER; currentGame++) {
             cout << "For Player " << (currentPlayer + 1) << ", enter score for game " << (currentGame + 1) << ": ";
             cin  >> playerScore[currentGame];
这可能是一个很好的起点


顺便说一句,这是您的编译器可能会警告您的;您可能需要打开更多警告(
-Wall-Wextra
是我最喜欢的
gcc
标志——您的编译器可能需要一些不同的东西)但它应该能够提醒您注意这一点。虽然您不需要修复每个编译器警告,但不要忽略它们——现代编译器中有数千年的编程经验。

您的循环不会检查最后一个游戏编号或玩家编号

仅仅保持控制台打开不就是
system(“pause”)
坏吗?您可以通过使用类似
std::cin.get()
getchar()
的方法来避免使用
system(“pause”)


您还制作了
playerScore
一个指针,并在它前面没有
*
的情况下使用它,因此您实际上试图获取它所指向的任何东西的地址(在本例中,没有任何东西——它甚至没有被分配)。

因此这里有一些问题:

  • 您从不为阵列分配任何空间。您的
    playerScore
    需要
    新的
  • cin>>playerScore[currentGame]
    将只写入数组标记0、1和2。此逻辑需要以某种方式组合currentPlayer和currentGame
  • playerAverage+=playerScore[currentGame]相同;
  • 使用完
    playerScore
    阵列后,您需要
    delete[]
    使用
    new
    分配的空间

您将输入存储在未知位置。我很惊讶您还没有遇到segfault

double*playerScore;
不一定声明数组,它是“指向double的指针”。您可以使用它在堆上创建数组(
playerScore=new double[SOME_SIZE];

除非您像使用任何其他未初始化变量一样实际告诉指针指向何处,否则无法知道它实际包含的内容。不同之处在于它不是将存储在那里的字节解释为int、double等。它被解释为内存地址,您尝试写入内存中的该位置


既然您知道需要存储多少个值,我就声明一个静态数组
double playerScore[SOME_SIZE]

如果cin成功执行,它下面的行将执行。我怀疑您看到了一些调试器工件,使它看起来像是cin正在执行,而不是下面的行。谢谢大家!我在发布这篇文章之后就在摆弄代码,不知怎的,我通过移动一些行使它工作起来。我想可能是这样一个侥幸。下次我编写类似的代码时,我肯定会把它搞砸:PSOMETIME的问题只是你忘了保存源文件,或者当你重建东西时它没有自动重新编译。这些事情可能会非常令人沮丧。我肯定会说Dev-C++内置的调试器很糟糕。我希望如此它可以像ValtualStudio一样显示我的价值和东西。也许它确实是,我还没有真正地看过。而且,确实,事情肯定会非常令人沮丧,但是到目前为止,C++比上学期C语言要容易得多。我也要说同样的话。除非指定专门调用I,否则不要声明指针用于以后分配。t、 你知道数组需要多少个元素,在堆栈上声明它。我在那里声明了指针,因为我被告知我应该在顶部声明所有稍后将要使用的元素,这样它在任何地方都可以访问。并且在最长的时间内(实际上只有5天)我以为我又在使用数组了,但我现在不这么认为了?不管怎样,指针指向playerScore[currentGame]中输入的值。我在那里用它来表示用户正在输入玩家的分数,不管游戏是什么数字。你可以用指针“创建”数组(或单个变量)使用<代码>新< /COD>。指针本身就是指针。我从类的未注释的样本中挑选出一些教科书和一些教科书材料,我原本以为我是用<代码> */COD>原始数组。我已经使用C++两个星期了。我也不擅长C数组,不幸的是,我仍然没有得到它们。+ 1 F或者警告。每一位老师都应该在第一堂课上就警告你不要打开警告。@R.Martinho:尽管在你花了一个小时试图找出这样的错误之前,这可能是一个很难接受的建议。:)如果我要分配数字,我会
double playerScore[PLAYER_NUMBER];