2

My question is this: how do I set the default value in my function of type Enum, to be one of the values in the Enum. This is my first experience with Enumerations and enumerated functions. I know a decent amount about Intro/Beginner C++.

Example.. Setting the Default value to return HANDHELD ?

Also any advice on making better/clearer questions would be appreciated.

ComputerType SellerList::ReadAndReturnComputerType() const
{
      char type[MAX_NAME_LEN];
      cin >> type;

      if(strcmp(type, "desktop") == 0)
         return DESKTOP;
      else if (strcmp(type, "laptop") == 0)
         return LAPTOP;
      else if(strcmp(type, "tablet") == 0)
         return TABLET;
      else if(strcmp(type, "handheld") == 0)
         return HANDHELD;

 }
3
  • 1
    else return HANDHELD; ? Commented Dec 2, 2013 at 21:41
  • That was one of my ideas. I had heard that there was a way you could do it before even beginning the if statement. If this would be the simplest way, then thank you! Commented Dec 2, 2013 at 21:43
  • Also, all if cases here end in a return, no reason to use "else". And just append a "return HANDHELD;" in the end. Commented Dec 2, 2013 at 22:15

2 Answers 2

1

Please don't do that kindda of stuff with strcmp and else if all over the place... You're doing c++ !

At least do something like the following piece of code. It's a lot more maintenable and with a bit of improvement you can handle the conversion enum/string and string/enum with the same map (you can even use bimap for this !).

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

enum ComputerType
{
    DESKTOP,
    LAPTOP,
    TABLET,
    HANDHELD
};

ComputerType ReadAndReturnComputerType()
{
    // I assume this is not multithread othewise there wouldn't be any cin
    static std::map<std::string, ComputerType> type { 
        { "desktop", DESKTOP },
        { "laptop", LAPTOP },
        { "tablet", TABLET },
        { "handheld", HANDHELD } };

    std::string input;
    std::cin >> input;

    auto it = type.find(input);

    // Don't forget to clean the input here !
    // something like:
    //
    // boost::trim(input);
    // boost::to_lower(input);

    if (type.end() == it)
    {
        return HANDHELD;
    }
    return it->second;
}
Sign up to request clarification or add additional context in comments.

5 Comments

I don't agree. Why make things slower and more complicated using complex containers when all you want to do is a simple mapping?
@TilmanVogel Slower ? When you're talking about std::cin and std::map access, this is not a consideration :). But if it's the case (and I do think map can be faster than a bunch of strcmp comparison but we have to test), unordered_map will certainly be better. More complicated ? mmmh matter of point of view. I find it a lot more readable and thus simpler. But I think it is a lot safer to avoid strcmp and if ... else if .. else if ... statements all over the place where you can easily miss a thing.
I agree that using a proper string class is much better, but I don't see an advantage in using a map instead of some simple if's. The story is different if you had a larger mapping where map or unordered_map could start shining but for such a small set of cases, the binary search in map or the extra hash calculation for unordered_map will just cause worse performance because the exact key comparison still needs to be done in both cases after finding a comparison candidate (by either binary search or hash lookup).
Ok for the perf, you're right the example is small. ;) The "objective" advantages I see for the map are: the easier extensibility (one line in the initializer) and the ease of the inversion of the conversion (yeah ok, easier with bimap but you could also do it with the map). What I fear most from such a code is from the char[] part that will one day come from outside the function without the \0. So yeah std::string definitely. std::map is bonus for me. :)
This was part of a 2 man project I was working on. We ran into the issue of 1 of 2 options to use and just wanted to get help with respect to the fact that I have no knowledge of Mapping or anything beyond my 5 months in the course. Thank you though, I'll have to do more research on how to use mapping and more advanced material.
0

if you want set the default value before if statement

ComputerType SellerList::ReadAndReturnComputerType() const
{
      ComputerType defType = HANDHELD;
      char type[MAX_NAME_LEN];
      cin >> type;

      if(strcmp(type, "desktop") == 0)
         type = DESKTOP;
      else if (strcmp(type, "laptop") == 0)
         type = LAPTOP;
      else if(strcmp(type, "tablet") == 0)
         type = TABLET;
      else if(strcmp(type, "handheld") == 0)
         type = HANDHELD;
      return defType;
 }

2 Comments

I appreciate it. This is exactly what I was looking for. I'll be sure to accept the answer in a few minutes. Thank you Bryan.
obviously the "type = " assignments should say "defType = " such that they actually have an effect. I would recommend to rename "type" -> "typeString" and "defType" -> "result".

Your Answer

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