2

I am trying to learn C++ (Beginner). But I wonder how I can make an enumeration of variables in statements like if.

Do I just put a comma between variables? What is the right syntax for this.. or is this all good?

#include <iostream>
using namespace std;

int main()
{
   int a, b, c, d, e;
   cin >> a >> b >> c >> d >> e;
   if (a, b, c > e && a, b, c > d) 
   {
      cout << a + b + c;
   }
}
JeJo
  • 30,635
  • 6
  • 49
  • 88
  • 2
    Not sure what you are asking, but this might help you: https://stackoverflow.com/questions/8781447/can-you-use-2-or-more-or-conditions-in-an-if-statement – NathanOliver Aug 04 '20 at 20:10
  • Well, you _can_ do this and since C++ has the [comma operator](https://stackoverflow.com/questions/54142/how-does-the-comma-operator-work) that laid low many beginners, it will actually compile. But it won't do what you want it to do. So, no, you can't do it in this way. – Lukas-T Aug 04 '20 at 20:14
  • Hint: Use `std::vector` instead of a jumble of variables. – tadman Aug 04 '20 at 20:15
  • That comma doesn't mean anything. This is junk code that compiles because it is syntactically valid, but it doesn't do what you want. If you're looking to assert that the first three entries are greater than some value then use [`std::all_of`](https://en.cppreference.com/w/cpp/algorithm/all_any_none_of) on a subset of your entries. – tadman Aug 04 '20 at 20:15
  • Another point to consider: Declare your `#include` definitions at the absolute top of the file, not after variable definitions. You should include whatever you need before you start declaring variables or functions. You'll also want to get out of the habit of declaring global variables immediately, put those in the function where they're used instead. – tadman Aug 04 '20 at 20:18
  • Thank you a lot, I understand now what I should do, I just pasted wrong "int a,b,c,d,e,f;". – Catana Matei Aug 04 '20 at 20:22

3 Answers3

4

No, there is nothing in C++ like that. You'll need to split each of those into their own statement, like:

if (a > e && b > e && c > e && a > d && b > d && c > d){

However, the logic here can be simplified.

If you want a > e AND a > d, then you only need to show that a is greater than the larger of e and d. The reverse is true for a, b and c. In other words, you only need to check that the smallest of a/b/c is greater than the largest of e/d.

So this can become:

if (min({a, b, c}) > max(e, d)){
scohe001
  • 15,110
  • 2
  • 31
  • 51
3

You can not do what you have tried there in the code.

The obvious way (beginner)way has been shown in the @scohe001's answer. However, when you learn at some point the templates and fold expressions, the following solution would be much compact and similar to what you have tried to do.

#include <iostream>
#include <utility>

template<typename... Args>
constexpr bool all_of_greater(const int lhs, Args&&... rhsArgs)
{
   return ((lhs < std::forward<Args>(rhsArgs)) && ...);
}

Now you could do similar to what you have done in the code:

if (all_of_greater(e, a, b, c) && all_of_greater(d, a, b, c))
{
   std::cout << a + b + c;
}

(See Online Demo)

JeJo
  • 30,635
  • 6
  • 49
  • 88
2

Here's a reworked approach where the "values" and the "targets" are split into two separate vector structures to ease comparison:

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

// Helper function to read an arbitrary number of entries into a vector
void read_n(std::vector<int>& list, const size_t n) {
  for (size_t i = 0; i < n; ++i) {
    int v;
    std::cin >> v;
    list.push_back(v);
  }
}

int main() {
  // Container to hold the values
  std::vector<int> values;

  read_n(values, 3);

  // Container to hold the targets
  std::vector<int> targets;

  read_n(targets, 2);

  // Ensure that for each target...
  for (auto target : targets) {
    // ...all of the values exceed that target...
    if (!std::all_of(values.cbegin(), values.cend(), [target](int i) { return i > target; })) {
      // ...or else it's a fail.
      return -1;
    }
  }

  // Use accumulate to compute the sum and display it.
  std::cout << std::accumulate(values.cbegin(), values.cend(), 0) << std::endl;

  return 0;
}

When writing code try and think in terms of structure and loops rather than just copy-pasting code to add more variables.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • @scohe001 I was going to rework this to use `argv` instead, but that seemed too ambitious. Let's see about that though. – tadman Aug 04 '20 at 20:30