0

I'm trying to do some of my C++ homework, but I seem to have run into an issue. I need to make it so that the user inputs 8 numbers, and those said 8 get stored in an array. Then, if one of the numbers is greater than 21, to output said number. The code is below, and it's kind of sloppy. Yes, first year C++ learner here :p

#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
  int sumVal = 0;             // For computing sum
  int prntSel = 0;            // For printing greater than 21

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


  for (i = 0; i < NUM_ELEMENTS; ++i) {
     cin >> userVals[i];
  }

  for (int i = NUM_ELEMENTS - 1; i > 21; i--)
     cout << "Value: " << sumVal << endl;

  // Determine sum
  sumVal = 0;

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

  cout << "Sum: " << sumVal << endl;

  return 0;
}
5
  • 6
    In what way does it not work? Commented Oct 16, 2014 at 18:45
  • 2
    Don't tag C for a C++ question, especially when you are learning the language (unless you really know C). The modern versions of the languages are different, and you should be learning the "natural" C++ way to do things. Commented Oct 16, 2014 at 18:48
  • @Overv It's just outputting 0 which I set it to when I declared it as a variable. Commented Oct 16, 2014 at 18:48
  • 1
    'i > 21' will never become true. i starts at 7 and is then decremented. Commented Oct 16, 2014 at 18:50
  • Your check for i > 21 is in the wrong place. Consider, are you checking the counter or the user entered value for 21? Commented Oct 16, 2014 at 18:50

6 Answers 6

3

Don't reinvent the wheel, use standard algorithms:

std::copy_if(std::begin(userVals), std::end(userVals),
             std::ostream_iterator<int>(std::cout, "\n"),
             [] (auto x) { return x > 21; });

I improved the rest of your program as well:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>

auto constexpr count = 8;

int main() {
    std::vector<int> numbers(count);

    std::cout << "Enter " << count << " integer values...\n";
    std::copy_n(std::istream_iterator<int>(std::cin), numbers.size(), numbers.begin());

    std::copy_if(numbers.begin(), numbers.end(),
                 std::ostream_iterator<int>(std::cout, "\n"),
                 [] (auto x) { return x > 21; });

    auto sum = std::accumulate(numbers.begin(), numbers.end(), 0);
    std::cout << "Sum: " << sum << '\n';

    return 0;
}

See it live on Coliru!

Sign up to request clarification or add additional context in comments.

1 Comment

+1 Your approach is sublime, but if I was his professor, then I would think that he is cheating!
2

Ok, I'm going to explain this to you and keep it simple. This loop

    `for (int i = NUM_ELEMENTS - 1; i > 21; i--)`

will never execute because in your first iteration you are checking if (NUM_ELEMENTS-1=7)>21. You are then decrementing i so this will take the series (6,5,4,...) and nothing would ever happen here.

If you have to sum the numbers greater than 21, which I presume is what you need then you will have to remove the above loop and modify your second loop to:

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

This way, you add the numbers in the array that are only greater than 21. The index of userVals is determined by the i variable which also acts as a counter.

Comments

1

You're on the right track. There's just a few things wrong with your approach.

#include <iostream>
#include <stdlib.h>

using namespace std;

int main() {
    const int NUM_ELEMENTS = 8;
    int userVals[NUM_ELEMENTS];
    int i = 0;
    int sumVal = 0;
    int prntSel = 0;
    int size = sizeof(userVals) / sizeof(int); // Get size of your array
                                               // 32/4 = 8 (ints are 4 bytes)
    cout << "Enter " << NUM_ELEMENTS << " integer values..." << endl;


    for (i = 0; i < NUM_ELEMENTS; ++i) {
        cin >> userVals[i];
    }

    for(int i = 0; i < size; i++) {
        if(userVals[i] > 21) { // Is number > 21?
            cout << userVals[i] << endl; // If so, print said number
            exit(0); // And exit
        }
        else
            sumVal += userVals[i]; // Else sum your values
    }

    cout << "Sum: " << sumVal << endl;

    return 0;
}

Comments

0
#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
  int sumVal = 0;             // For computing sum
  int prntSel = 0;            // For printing greater than 21

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


  for (i = 0; i < NUM_ELEMENTS; ++i) {
     cin >> userVals[i];
  }

//  for (int i = NUM_ELEMENTS - 1; i > 21; i--)
//     cout << "Value: " << sumVal << endl;

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

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

  cout << "Sum: " << sumVal << endl;

  return 0;
}

Comments

0

Try

for (int i = NUM_ELEMENTS - 1; i > 21; i--)
     cout << "Value: " << sumVal << endl;

to

for (i = 0; i < NUM_ELEMENTS; ++i) {

    if(userVals[i] > 21)
        cout << "Value: " << userVals[i] << endl;
}

This line isnt needed as well, as you arent using it.

int prntSel = 0;            // For printing greater than 21

3 Comments

Tried doing that, and the value is still set to zero. I'll include a link, and the code again. link link
That seems to work. If you could tell me what I was doing wrong, that would be much appreciated :)
You were printing you sumVal instead of the UserVals number the player put in. You were also setting i to the number of elements he entered - 1. So 4, then checking to see if it was over 21. This could never occur so your loop wasn't working. All you needed to do was iterate over your numbers again and check the actual values against 21.
0
for (int i = NUM_ELEMENTS - 1; i > 21; i--)
     cout << "Value: " << sumVal << endl;

Here you are printing the value of sumVal, not the value of the array in the position i. The line should be:

cout << "Value: " << usersVals[i] << endl;

Also that that your for is not doing what you think it does. for doesn't use the condition you gave to decide if will execute the current iteration or not, it uses the condition to decide if the loop should continue or not. So when you put i > 21, means that it will continue running while i is bigger than 21. To achieve your goal, you should make a test (if statement) inside the loop.

The final result it would be:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.