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?

Jenna R
  • 31
  • 4

4 Answers4

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;
}
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
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.
Pradhan
  • 16,391
  • 3
  • 44
  • 59
  • 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). – OznOg Oct 28 '17 at 12:17
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.

Drewen
  • 2,806
  • 1
  • 15
  • 12
  • That's bad idea. Standard doesn't guarantee that res would be `int`, and `scanf` is not type safe. @Pradhan provided much cleaner answer – myaut Mar 05 '15 at 23:28
  • 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). – Drewen Mar 05 '15 at 23:33
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.

Community
  • 1
  • 1
Marcin
  • 215,873
  • 14
  • 235
  • 294
  • 1
    Ugh, nasty macro hackery, I think the std::map solution is better – paulm Mar 05 '15 at 23:35
  • 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. – jplatte Mar 05 '15 at 23:39
  • 2
    the fact that it does not look "pretty" or has CAPSLOOK, it does not make it invalid answer. Its an alternative to other answers. – Marcin Mar 06 '15 at 00:12