用C++计算两个不同的平均值
Calculating two different averages in C++
我的任务是计算保龄球的平均值。我有五名球员,每个球员三场比赛。我目前有两个循环在运行,一个用于玩家,另一个用于游戏编号。我需要显示每个循环结束时球员的平均值,以及循环结束时球队的平均值。
我修复了我的代码,并用下面的新代码替换了我的旧代码。在我查看这里查看每个人的评论等之前,我一直在玩它,到那时我已经解决了它。
但谢谢大家!
#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]
- 如何在C++中从两个单独的for循环中添加两个数组
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- 如何返回一个类的两个对象相加的结果
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 两个字符串在 c++ 中不相等
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 两个文件使用彼此的功能-如何解决
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 如何确保在使用基于布尔值的两个方法之一调用方法时避免分支预测错误
- 停止cmake target_link_libraries将插件中静态库的两个对象文件链接到静态库本身
- 将fold表达式与std::一起用于两个元组
- 如何在C++中比较两个char数组
- 给定两个偶数,求出它们之间所有偶数的平方和
- 两个字符串的平均值C++
- 用C++计算两个不同的平均值
- 用两个小数求3个数字的平均值(不舍入!)
- 迭代器的两个 ** 的平均值
- 如何有效地找到最接近两个整数以100,000为增量的平均值的最大整数