1

The essence of the task: to choose the best screen resolution for the user, based on his data. I need to ompare user value with value from ENUM and return the appropriate one. For example:

enum class Resolutions {
    V720_height = 720,
    V720_width = 1280,
    V1080_height = 1080,
    V1080_width = 1920
};

int main() {
    int user_height = 1200;
    int user_width = 430;
    // I know this does not work, just an example.
    std::for_each(Resolutions.begin(), Resolutions.end(), [](Resolutions tmp) {
        if (static_cast<int>(tmp) > user_height) {
            std::cout << tmp << " - is better resolution\n";
        }
    });
}

I need a good idea, how can this be implemented?

Yaniboze
  • 73
  • 6
  • 1
    You can't iterate an enum, but perhaps [How can I iterate over an enum?](https://stackoverflow.com/questions/261963/how-can-i-iterate-over-an-enum) gives you some ideas. [Allow for Range-Based For with enum classes?](https://stackoverflow.com/questions/8498300/allow-for-range-based-for-with-enum-classes) looks to more directly handle your case. – user4581301 May 25 '20 at 19:48
  • It looks like your problem originates from your decision to use an enumeration to create symbolic constants. Why did you choose this over other options for symbolic constants? Are those reasons more significant than ease of iteration? – JaMiT May 25 '20 at 20:27

2 Answers2

3

Using enums is not the best way to do that. I recommend you to use std::map if you need to name the different resolutions.

#include <map>
#include <string>
#include <iostream>

struct Resolution {
    int height;
    int width;
};

const std::map<std::string, Resolution> resolutions = {
    { "V720", {720, 1280} },
    { "V1080", {1080, 1920} }
};

int main() {
    int user_height = 1200;
    int user_width = 430;

     for (auto& [key, value] : resolutions) {
        if (value.height > user_height &&
            value.width > user_width) {
            std::cout << key << " - is better resolution\n";
        }
    }
}
P. Milev
  • 374
  • 2
  • 12
0

You could do something like this (note: I'm using C-style casts for brevity):

enum class Resolutions {
    V720_height = 720,
    V720_width = 1280,
    V1080_height = 1080,
    V1080_width = 1920
};

const int heights [] = { (int) Resolutions::V720_height, (int) Resolutions::V1080_height };

int main() {
    int user_height = 1000;
    for (auto h : heights) {
        if (h > user_height) {
            std::cout << h << " - is better resolution\n";
        }
    };
}

Live demo

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48