enum - get key from char value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jan Spatina
    New Member
    • Mar 2007
    • 9

    #1

    enum - get key from char value

    Hello,
    I have this type:

    enum days{sun = 1, mon, tue, wed, thu, fri, sat};

    and I have char in which is stored a name of the day:

    char* day = "wed";

    How can I recieve number of the day?
    I tried something like:

    int number = days(*day);
    cout<<number;

    but this won't work :(
    thanks
  • bluesteel
    New Member
    • Mar 2007
    • 84

    #2
    Originally posted by Jan Spatina
    Hello,
    I have this type:

    enum days{sun = 1, mon, tue, wed, thu, fri, sat};

    and I have char in which is stored a name of the day:

    char* day = "wed";

    How can I recieve number of the day?
    I tried something like:

    int number = days(*day);
    cout<<number;

    but this won't work :(


    thanks
    could you be more accurate? What problems do you have? Does it compile?? What errors does it return???

    Comment

    • Jan Spatina
      New Member
      • Mar 2007
      • 9

      #3
      Yes, let's make it simplier:

      I have only this:

      int sun = 1;
      int mon = 2;
      int tue = 3;

      char* day = "mon"; .. this might be given by user input during run of program

      I want to print something like:

      cout<<"number of "<<day<< " is: "<< ? - what here - ?

      -thanks-

      Comment

      • AdrianH
        Recognized Expert Top Contributor
        • Feb 2007
        • 1251

        #4
        Originally posted by Jan Spatina
        Yes, let's make it simplier:

        I have only this:

        int sun = 1;
        int mon = 2;
        int tue = 3;

        char* day = "mon"; .. this might be given by user input during run of program

        I want to print something like:

        cout<<"number of "<<day<< " is: "<< ? - what here - ?

        -thanks-
        There are a few ways you could do this, two of which would be to either make a class, or make a function. The latter is probably the easist and the former overkill, but then again the latter may not be sufficent and the former may be the right route. It all depends on what you are doing and why.


        Adrian

        Comment

        • Jan Spatina
          New Member
          • Mar 2007
          • 9

          #5
          Originally posted by AdrianH
          There are a few ways you could do this, two of which would be to either make a class, or make a function. The latter is probably the easist and the former overkill, but then again the latter may not be sufficent and the former may be the right route. It all depends on what you are doing and why.


          Adrian
          Any examples?

          thanks

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            Use a switch structure. If the first letter of the user's input is m, then the day is monday. If the first letter is w, then the day is wednesday. If the first letter is t, then it is either tuesday or thursday. If you embed this switch inside a function, it will make your code much easier to read.

            Comment

            • gpraghuram
              Recognized Expert Top Contributor
              • Mar 2007
              • 1275

              #7
              HI,
              If you are using C++,try this.
              Code:
               int get_day_of_Week(string _day)
              {
                map<string,int> day_map;
                day_map["sun"]=1;day_map["mon"]=2;...day_map["sat"]=7;
                return day_map[_day];
              }
              int main()
              {
                cout<<get the input in string;
                cout<<get_day_of_Week(day);
              }
              I have put only a pseudocode.
              If needed i can add more info.
              Thanks
              Raghuram

              Comment

              • Jan Spatina
                New Member
                • Mar 2007
                • 9

                #8
                Originally posted by gpraghuram
                HI,
                If you are using C++,try this.
                Code:
                 int get_day_of_Week(string _day)
                {
                  map<string,int> day_map;
                  day_map["sun"]=1;day_map["mon"]=2;...day_map["sat"]=7;
                  return day_map[_day];
                }
                int main()
                {
                  cout<<get the input in string;
                  cout<<get_day_of_Week(day);
                }
                I have put only a pseudocode.
                If needed i can add more info.
                Thanks
                Raghuram

                Thanks guys, that helped.
                jan

                Comment

                Working...