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

Calculating two different averages in C++

本文关键字:两个 平均值 C++ 计算      更新时间:2023-10-16

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

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

但谢谢大家!

#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

但是,对于仍在那里的任何人来说,有没有一种方法可以让终端保持打开状态,而不必使用"sys("PAUSE");"?我真的很讨厌使用它。

您正在声明double* playerScore,但我看不出您在哪里分配存储。也许你在改写什么。

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

system("pause")只是打开控制台不好吗?您可以通过使用类似std::cin.get()getchar()的内容来避免使用system("pause")

您还将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];

当您写入playerScore[currentGame]时,您正在写入从未分配的内存。我不知道你在写什么,但这不是你写的。

您应该为playerScore分配内存。你必须决定分配内存的最佳方式,但类似于:

double playerScore[PLAYER_NUMBER];

这可能是一个很好的起点。

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

所以这里有一些问题:

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

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

double* playerScore;不一定要声明一个数组,它是一个"指向双精度的指针"。您可以使用它在堆上创建一个数组(playerScore = new double[SOME_SIZE];)。

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

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