Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C++
#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];
   }
   
   // Determine values greater than 21
   for (i = 0; i <= NUM_ELEMENTS; ++i) {
      if   //This is where I am stuck.....
        cout << endl << userVals[i] << endl;
   }
   
   return 0;
}
Posted
Updated 14-Oct-14 13:22pm
v6

1 solution

You are giving the entered inputs to every place in the array with the first for. Which means userVals[i] is the place where you are saving the info.
To know if the entered numbers are bigger than 21, you have to compare them (logically) and you know where they are saved. Don't you? At the end, a position of the array can be considered as the variable you are asking in that moment.

I mean, in the concept of comparison there is no difference with

C++
int a = 1;
if (a > 0)

//  or

int a = 1
int b = 2
if (a > b)


in conclusion...

C++
for (i = 0; i <= NUM_ELEMENTS; ++i) {
   if (userVals[i] > 21)  // or if (userVals[i] >= 22)
     cout << endl << userVals[i] << endl;
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900