Thread: Using strcmp with enumerations.

  1. #1
    1479
    Join Date
    Aug 2003
    Posts
    253

    Using strcmp with enumerations.

    Just curious as to how this would work. This is what I have come up with so far.
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    int dailysum,costofUpkeep,cash_made,weeklysum;
    char timecheck[10];
    
    cout <<"How often would you like to check your machines?" <<endl;
    cin.getline(timecheck,10)
    enum time {"daily","weekly","monthly","yearly"}
        switch((!strcmp(time,timecheck))
            {
            case 0:
                    dailysum=costofUpkeep-cash_made;
                    cout <<dailysum;
                    break;
            case 1: 
                    weeklysum = (cash_made * 7)-costofUpkeep;
                    cout <<weeklysum;
            }
    }
    Can't get this to work. THere are only 4 errors:
    parse, syntax, and At Global
    Knowledge is power and I want it all

    -0RealityFusion0-

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    enum time {"daily","weekly","monthly","yearly"}

    Look at the following code--it might prove enlightening:

    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
    	enum Weekday{Mon, Tues, Wed, Thurs, Fri, Sat, Sun};
    
    	Weekday today = Mon;
    	cout<<today<<endl;
    
    	today = Tues;
    	cout<<today<<endl;
    
    	return 0;
    }
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	enum Weekday{Mon=10, Tues=Mon+2, Wed=Tues+2, 
    		Thurs=Wed+2, Fri=Thurs+2, Sat=Fri+2, Sun=Sat+2};
    
    	Weekday today = Wed;
    	cout<<today<<endl;
    
    	return 0;
    }
    Last edited by 7stud; 08-19-2003 at 03:08 PM.

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Enums are solely used for code readability. You can't compare an enum with a string unfortunately nor can you print an enum. Well, you CAN print an enum, but it'll print out the integer value of it, NOT the name of it.

  4. #4
    1479
    Join Date
    Aug 2003
    Posts
    253
    This is hard to explain. 7stud I am not trying to make an enumeration, I can do that. What I am trying to do is make an enumeration based on user input. The reason I put daily,weekly, monthy etc.... in "" is becuase that is what the user will have to type in. They will be asked how they want to calculate something. Either by day,week,month,year. I didn't want a boring options list so I let the user type in what they wanted. Now, depending on what they typed, wether it be day,week,month,year it would go do a certain part and calculate a formula.......if that makes any sense.
    Knowledge is power and I want it all

    -0RealityFusion0-

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >What I am trying to do is make an enumeration based on user input.
    You're trying to solve the wrong problem. Really, you are.

    >I didn't want a boring options list so I let the user type in what they wanted.
    Bad idea. Options are very predictable, users are quite the opposite. It's easier and simpler to parse options 1, 2, and 3 as opposed to trying to parse the gobbledygook users feel the need to give you.

    >Now, depending on what they typed, it would go do a certain part and calculate a formula
    You can and should use either some if statements, or a data structure:
    Code:
    getline(cin, input);
    
    if (input == "daily")
        ...
    else if (input == "weekly")
        ...
    else if (input == "monthly")
        ...
    else if (input == "yearly")
        ...
    else
        // Invalid input
    or
    Code:
    getline(cin, input);
    
    if (mymap.find(input) != mymap.end())
        mymap[input]();
    Often the simplest seeming solution is far from simple. Dig a bit and you can find the more elegant ones.
    My best code is written with the delete key.

  6. #6
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    You could put all choices in one string and offset each at x*10 bytes. Than you could switch like case 0, case 10, case 20. Well, that would be possible. Its a very good way to produce awful code that hurts when reading it

    You could also use a map, use "find" to find the users input in the map and use switch on the iterator "find" returned.

    Best thing however would be to use if... else statements.

    edit: ok, what prelude said

  7. #7
    1479
    Join Date
    Aug 2003
    Posts
    253
    Well after toying with it forever this is what I have come up with:
    Code:
    //Machine Tracking beta(version 1.0)
    
    #include <iostream>
    using namespace std;
    
    class Machines
        {
        public:
            char Machine_income(char timecheck,int cash_made);
            int Machine_upKeepCost();
            int Machine_profit();
            int Machine_GetType();
            char Machine_GetLocation();
        private:
            char Machine_type;
            char Machine_location;
        };
    XXXint Machines::Machine_income(char timecheck,int cash_made)//,int UpkeepCost)
        {
         int daily_average,weekly_average,monthly_average,yearly_average,NumberofDays;
         
          //Change this to an numeration
            if (!strcmp("daily",timecheck))
                   {
                    cout <<"In one day the machine made: " <<cash_made <<endl;
                    cout <<"How many days would you like to calculate?" <<endl;
                    cout <<"Please note: if the amount of days you want to calculate is over 6";
                    cout <<" just use the weekly calculations." <<endl;
                    cin >>NumberofDays;
                    daily_average = (NumberofDays * cash_made);//-UpkeepCost;
                    cout <<"In " <<NumberofDays <<"the machine made: " <<daily_average;
                   }
                    if (!strcmp("weekly",timecheck))
                         {
                         weekly_average = (cash_made * 7);//-UpkeepCost;
                         cout <<weekly_average <<endl;
                         }
                         if (!strcmp("monthly",timecheck))
                              {
                               monthly_average = (cash_made * 30);//-UpkeepCost;
                               cout <<monthly_average;
                               } 
                               if (!strcmp("yearly",timecheck))
                              {
                               yearly_average = (cash_made * 30);//-UpkeepCost;
                               cout <<yearly_average;
                               }                                       
         
         }
         
    int main()
        {
            char timecheck[10];
            int cash_made;
            
            Machines p;
            cout <<"To calculate the income produced by your machine(s)";
            cout <<" please select how often you will check them." <<endl;
            cin.getline(timecheck,20);
            cout <<"How much money did it make in that amount of time?" <<endl;
            cin >>cash_made;
            //cout <<"How much does it cost to fill up all of your machines?" <<endl;
            //cin >>UpkeepCost;
            //$230.00 for candy  // $350.00 for toys
            cout <<p.Machine_income(char timecheck,int cash_made);
            system("pause");
        }
    This is what I got so far. 9 errors though. I think where the big red x's are is what 1 of the problems may be. I am not sure what I should put there seeing as it is passed a char and an int.
    Knowledge is power and I want it all

    -0RealityFusion0-

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
      int dailysum,costofUpkeep,cash_made,weeklysum;
      char timecheck[10];
      cout <<"How often would you like to check your machines?" <<endl;
      cin.getline(timecheck,10);
      enum time {daily,weekly,monthly,yearly};
      switch((!strcmp(time,timecheck))
      {
            case 0:
                    dailysum=costofUpkeep-cash_made;
                    cout <<dailysum;
                    break;
            case 1: 
                    weeklysum = (cash_made * 7)-costofUpkeep;
                    cout <<weeklysum;
      }
    }
    The last error is in your switch statment...

  9. #9
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    Char timecheck is just one char. use char[] or *char instead. Also your if identination is wrong =)


    Code:
            if (!strcmp("weekly",timecheck))
            {
                weekly_average = (cash_made * 7);//-UpkeepCost;
                cout <<weekly_average <<endl;
            }
            if (!strcmp("monthly",timecheck))
            {
                monthly_average = (cash_made * 30);//-UpkeepCost;
                cout <<monthly_average;
            } 
            if (!strcmp("yearly",timecheck))
            {
              yearly_average = (cash_made * 30);//-UpkeepCost;
              cout <<yearly_average;
            }
    Since this is the C++ Board, you should use real strings and not those lousy slow zero-terminated thingys some people call strings =)
    Last edited by darksaidin; 08-19-2003 at 04:10 PM.

  10. #10
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Can you spot an error ? A hint, return type...
    Code:
    char Machine_income(char timecheck,int cash_made);
    
    int Machines::Machine_income(char timecheck,int cash_made)
    In your ocde timecheck is a char, strcmp used char* as arguments.
    Code:
    if (!strcmp("daily",timecheck))
    Wouldnt it be a good idea to use either 10 or 20 ?
    Code:
      char timecheck[10];
      // code..
      cin.getline(timecheck,20);

    What is this ? Shouldnt there be some values in there ?
    Code:
    cout <<p.Machine_income(char timecheck,int cash_made);

  11. #11
    Registered User
    Join Date
    May 2002
    Posts
    66
    You can always use C++ and maps to solve the problem:

    Code:
    #include <iostream>
    #include <map>
    
    class EnumDayOfWeek
    {
    public:
      enum enumName { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday };
      EnumDayOfWeek() {}
    
      static void initialize();
    
      static const char *get(enumName e)
      {
        return m_map[e].c_str();
      }
    
    private:
      static std::map<enumName, std::string> m_map;
    };
    
    std::map< EnumDayOfWeek::enumName, std::string > EnumDayOfWeek::m_map;
    void EnumDayOfWeek::initialize()
    {
      m_map.clear();
      m_map[Monday] = "Monday";
      m_map[Tuesday] = "Tuesday";
      m_map[Wednesday] = "Wenesday";
      m_map[Thursday] = "Thursday";
      m_map[Friday] = "Friday";
      m_map[Saturday] = "Saturday";
      m_map[Sunday] = "Sunday";
    }
    
    class InitEnumDayOfWeek { public: InitEnumDayOfWeek() { EnumDayOfWeek::initialize(); } } theInitEnumDayOfWeek;
    
    int main(int argc, char *argv[])
    {
      EnumDayOfWeek::enumName day = EnumDayOfWeek::Tuesday;
    
      std::cout << EnumDayOfWeek::get(day) << std::endl;
    
      return 0;
    }

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    "7stud I am not trying to make an enumeration, I can do that."

    As everyone who has responded to this thread knows, you certainly don't understand what an enumeration is. You cannot compare an enumerated type to a string--that was the point of the code I posted. Do you see why?
    Last edited by 7stud; 08-19-2003 at 04:49 PM.

  13. #13
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    Originally posted by 7stud
    "7stud I am not trying to make an enumeration, I can do that."

    As everyone who has responded to this thread knows, you certainly don't understand what an enumeration is. You cannot compare an enumerated type to a string--that was the point of the code I posted. Do you see why?
    Well, you could compare them by using macros (#) but that still doesn't make the enum a string.


    mhm, did I just post spam? uh oh... time to get some sleep

  14. #14
    1479
    Join Date
    Aug 2003
    Posts
    253
    I don't know what you mean by this statement:

    "In your ocde timecheck is a char, strcmp used char* as arguments."

    Code:
    if (!strcmp("daily",timecheck))
    Please explain.
    Knowledge is power and I want it all

    -0RealityFusion0-

  15. #15
    1479
    Join Date
    Aug 2003
    Posts
    253
    Got it working......thanks for all you people who helped. If anyone wants the code to see how it runs just ask me and I will post it.
    Knowledge is power and I want it all

    -0RealityFusion0-

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fucntion returns -1, Why?
    By Taper in forum C Programming
    Replies: 16
    Last Post: 12-08-2008, 06:30 PM
  2. help with switch statement
    By agentsmith in forum C Programming
    Replies: 11
    Last Post: 08-26-2008, 04:02 PM
  3. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 12:07 PM
  4. help with strcmp
    By blork_98 in forum C Programming
    Replies: 8
    Last Post: 02-21-2006, 08:23 PM
  5. strcmp
    By kryonik in forum C Programming
    Replies: 9
    Last Post: 10-11-2005, 11:04 AM