如何显示大于某个值的数字?

时间:2014-10-15 18:11:12

标签: c++ arrays

我试图将其设置为仅显示大于21的数字,但我正在努力。

这是我到目前为止的代码。

#include <iostream>
using namespace std;

int main() {
   const int NUM_ELEMENTS = 8; // Number of elements
   int userVals[NUM_ELEMENTS]; // User numbers
   int i = 0;                  // Loop index

   // Prompt user to populate array
   cout << "Enter " << NUM_ELEMENTS << " integer values..." << endl;

   for (i = 0; i <= NUM_ELEMENTS; ++i) {
      cout << "Value: " << endl;
      cin >> userVals[i];
   }

   for (i = 0; i < NUM_ELEMENTS; ++i) {
      cout << userVals[i] << " ";
   }

   return 0;
}

1 个答案:

答案 0 :(得分:2)

for (i = 0; i < NUM_ELEMENTS; ++i)
{
  if(userVals[i] > 21)
    cout << userVals[i] << " ";
}