3
#include <stdlib.h>
#include <stdio.h>
using namespace std;



void main(){
    char *resolutions[] = { "720x480", "1024x600", "1280x720", "1920x1080" };

    int x = 0;

    enum ResMode
    {
        p480,
        p600,
        p720,
        p1080
    }; 
    ResMode res = p480;

    printf("\nPlease enter the resolution you wish to use now by entering a number");
    printf("\n480p[0], 600p[1], 720p[2], 1080p[3]");
    gets(res);

    printf("\nThe resolution you have selected is %s", resolutions[res]);

}

so basically i want to be able to press 1 and have it select p600 from enum and out put it as 1024x600 in the next line. I am getting a type conversion error. How can i fix this?

2
  • 1
    Don't use gets. It no longer exists as of the most recent C++ standard. Commented Mar 5, 2015 at 22:56
  • @Brian: Better: Don't acknowledge there ever was gets. It was eliminated from the newest C and C++ standards for being a blatant bug. Commented Mar 6, 2015 at 0:02

4 Answers 4

7

Looks like you want to associate some items with other items. Usually associations are described in lookup tables or maps.

std::map<ResMode, std::string> map_table =
{
  {p480,     string("720x480")},
  {p600,     string("1024x600")},
  {p720,     string("1280x720")},
  {p1080,    string("1920x1080")},
};

int main(void)
{
  cout << map_table[p480] << "\n";
  return EXIT_SUCCESS;
}

Likewise, you can map menu selections to enums.

Edit 1

std::map<unsigned int, ResMode> selection_map =
{
  {0, p480}, {1, p600}, {2, p720}, {3, p1080},
};

int main(void)
{
  cout << "\n"
       << "Please enter the resolution you wish to use now by entering a number\n"
       <<"480p[0], 600p[1], 720p[2], 1080p[3]";
  unsigned int selection = 0;
  cin >> selection;
  if (selection < 4)
  {
    Resmode resolution_index = selection_map[selection];
    cout << "You chose: "
         << map_table[resolution_index]
         << "\n";
  }
  return EXIT_SUCCESS;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Why the downvote? It would be nice if these downvoters would leave a comment.
The question is tagged as C++, so I am using C++ features.
For OP who writes using namespace std; and gets() your approach might be a culture shock. (I'm not downvoter)
2

int's are not implicitly convertible to an enum. You will have to read in an int and then cast it yourself. Example,

int resInt;
scanf("%d", &resInt);
res = static_cast<ResMode>(resInt);//Note that this does not do bound checking.

1 Comment

I quite dislike this way of doing as the boundaries should be checked before the conversion. If not, you'll end up with a strange function taking a ResMode as parameter and checking it is actually one... To e the simple fact that you have a ResMode should guaranty that it is valid. You probably need a function for performing the cast with boundaries checks done (maybe what you meant here, but not that clear).
2

You can use "scanf" instead of "gets", something like this:

scanf("%d",&res); // I recommend use scanf_s

Or the iostream library with std::cin. But after taking the input, always, check if the input is the correct one.

2 Comments

That's bad idea. Standard doesn't guarantee that res would be int, and scanf is not type safe. @Pradhan provided much cleaner answer
That's true, having a integer and casting should complete the answer. BTW the C++ compiler provides an automatic conversion from enum to int (not the inverse).
0

As otehrs pointed out, there is no direct way of doing this. However, there are some recipes/tricks that you can use. I modified your code as follows:

#include <stdlib.h>
#include <stdio.h>


#define SOME_ENUM(DO) \
    DO(_720x480)  \
    DO(_1024x600) \
    DO(_1280x720) \
    DO(_1920x1080)

#define MAKE_ENUM(VAR) VAR,
enum class RESOLUTIONS
{
    SOME_ENUM(MAKE_ENUM)
};

#define MAKE_STRINGS(VAR) #VAR,
const char* const
RESOLUTION_NAMES[] =
{
    SOME_ENUM(MAKE_STRINGS)
};



const char *
GET_RESOLUTION_NAME(RESOLUTIONS type)
{
    return RESOLUTION_NAMES[static_cast<int>(type)];
}

int
GET_RESOLUTION_VALUE(RESOLUTIONS type)
{
    return static_cast<int>(type);
}

RESOLUTIONS
GET_RESOLUTION(int i)
{
    return static_cast<RESOLUTIONS>(i);
}



using namespace std;



int main(){


    printf("\nPlease enter the resolution you wish to use now by entering a number");

    printf("\n480p[0], 600p[1], 720p[2], 1080p[3]");

    int res_type;

    cin >> res_type;


    RESOLUTIONS selected_res = GET_RESOLUTION(res_type);

    printf("\nThe resolution you have selected is %s\n\n", GET_RESOLUTION_NAME(selected_res));

    return 0;

}

Sorry for not providing an explanation, as I have to go now. This recipe can be found here. The code works and compiles for c++11.

3 Comments

Ugh, nasty macro hackery, I think the std::map solution is better
I agree, this doesn't look very nice. Besides the macro, it has random white space and a lot of CAPSLOCK. It's also a lot of code for such a simple problem.
the fact that it does not look "pretty" or has CAPSLOOK, it does not make it invalid answer. Its an alternative to other answers.

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.