c++ - 在确定数组的总数和平均值时,如何阻止将哨兵值读入数组?

标签 c++ arrays while-loop sentinel

我不确定如何阻止我的标记值被读入我的数组,这会导致我的总计和平均计算出错。谁能帮忙?

这是 while 循环:

while (grade != -1)
{
    cin >> grade;
    gradesArray[count] = grade;
    total += gradesArray[count];
    average = total / count;
    count++;
}

cout << "You have entered " << count << " grades." << endl;
cout << "The average of these grades is " << average << endl;

最佳答案

while (std::cin >> grade && grade != -1)

关于c++ - 在确定数组的总数和平均值时,如何阻止将哨兵值读入数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38318531/

相关文章:

c++ - 我应该如何更正此 priority_queue 比较功能?

c++ - 这是最新的 C++ 标准版本,我可以从哪里下载它

c++ - 为什么 accumulate 比简单的 for 循环更快?

c - 一维数组中的矩阵相乘

linux - 逐行读取文件 fork/exec - Perl

c++ - 为什么 QLabel 会过早换行?

c - 从文件读取以创建数组扫雷代码

将结构数组的索引与 NULL 进行比较,而不是评估为 true;

python - 从 Python 读取输入并在 while 循环中打印出来

c++ - 使用 while 循环计算数学常数 e