12 Replies - 1793 Views - Last Post: 21 January 2011 - 06:22 PM Rate Topic: -----

#1 shadowstep0705  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 48
  • Joined: 26-May 10

string enum comparison

Posted 21 January 2011 - 09:58 AM

Hi everyone,
I am making a program which works with option flags, so I want to make an enum table with strings with all the commands in, then I can compare if the given option is in the list.

How should I do this?
Is This A Good Question/Topic? 0
  • +

Replies To: string enum comparison

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: string enum comparison

Posted 21 January 2011 - 10:04 AM

Language? (computer support really isn't the place)

As described, I'd just a plain ol' array of strings as your lookup table. An enum (again depending on language) could just be an integer equivalent or a full blown object.
Was This Post Helpful? 0
  • +
  • -

#3 shadowstep0705  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 48
  • Joined: 26-May 10

Re: string enum comparison

Posted 21 January 2011 - 10:23 AM

Sorry, the language is c++, so you would make an array?

PS: can someone replace this topic because I posted in wrong section :s

This post has been edited by shadowstep0705: 21 January 2011 - 10:24 AM

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: string enum comparison

Posted 21 January 2011 - 10:34 AM

Show an example of what you're trying to achieve. If you're using C++, it sounds like you should consider using a std::map<std::string, std::string>.
Was This Post Helpful? 1
  • +
  • -

#5 shadowstep0705  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 48
  • Joined: 26-May 10

Re: string enum comparison

Posted 21 January 2011 - 10:42 AM

View PostJackOfAllTrades, on 21 January 2011 - 10:34 AM, said:

Show an example of what you're trying to achieve. If you're using C++, it sounds like you should consider using a std::map<std::string, std::string>.

Well
#include <iostream>
#include "main.h"
using namespace std;

int main(int argc, char* argv[])
{
 // here I need a compare method which will compare argv[] to my command[] string array
return 0;
}


This post has been edited by shadowstep0705: 21 January 2011 - 10:44 AM

Was This Post Helpful? 0
  • +
  • -

#6 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: string enum comparison

Posted 21 January 2011 - 11:00 AM

View Postshadowstep0705, on 22 January 2011 - 03:42 AM, said:

// here I need a compare method which will compare argv[] to my command[] string array


So show us the code you have written that tries to do that.
Was This Post Helpful? 0
  • +
  • -

#7 shadowstep0705  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 48
  • Joined: 26-May 10

Re: string enum comparison

Posted 21 January 2011 - 11:16 AM

View Postjanotte, on 21 January 2011 - 11:00 AM, said:

View Postshadowstep0705, on 22 January 2011 - 03:42 AM, said:

// here I need a compare method which will compare argv[] to my command[] string array


So show us the code you have written that tries to do that.


Well, that's why I came here, I absolutely have no idea how to do that :s
Was This Post Helpful? 0
  • +
  • -

#8 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: string enum comparison

Posted 21 January 2011 - 11:25 AM

Okay, what can you do?

Do you know how to compare two strings?
Do you know how to use a command line argument?

What parts of this do you know?

Write the code you do know.
Was This Post Helpful? 0
  • +
  • -

#9 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: string enum comparison

Posted 21 January 2011 - 12:34 PM

Well since you didn't give us much to go on I put this example together using a technique called X-Marcos.

I use the preprocessor to define a table of macro calls to the abstract macro named X.

X() is defined three times. The first time to format table enteries into the syntax for an enum, next for an array of string for the values (prefixed with a "-"), and finally an array of strings for the names.

here is the program
#include <iostream>
#include <cstring>

using namespace std;

#define XTABLE  \
X(c, compress)  \
X(f, free)      \
X(C, compile)   \
X(z, zero)      \
X(m, milling)   \
X(x, eXciting)    

#define X(value, name) ARG_ ## value,
enum ARG_MAP {
    ARG_NONE,
    XTABLE
    ARG_COUNT //Tells us how many items are in the table -- thus the size of our arrays
};
#undef X

#define X(value, name) "-" #value,
const char* ARG_VALUES[] = {
    "", //Aligns with ARG_NONE
    XTABLE
};
#undef X

#define X(value, name) #name,
const char* ARG_NAMES[] = {
    "Invalid Argument", //ARG_NONE
    XTABLE
};
#undef X


ARG_MAP getArg(const char* arg) {
    for (int i = 1; i < ARG_COUNT; ++i) {
        if (0 == strcmp(arg, ARG_VALUES[i])) { return (ARG_MAP)i; }
    }
    return ARG_NONE;
}


int main(int argc, char* argv[]) {
    if (argc < 2) {
        cout << "Please add some options" << endl;
        return 1;
    }
    for(int i = 1; i < argc; ++i) {
        ARG_MAP arg = getArg(argv[i]);
        cout << "Argument: " << ARG_NAMES[arg];
        switch(arg) {
            case ARG_c:
                cout << ": doing compression...." << endl;
                break;
            case ARG_f:
                cout << ": freeing memory...." << endl;
                break;
            case ARG_C:
                cout << ": Compiling...." << endl;
                break;
            case ARG_NONE:
                cout << ": Unknown command line argument!" << endl;
                break;
            default:
                cout << ": No operation...." << endl;
                break;
        }
    }
    return 0;
}


here is the output:
> "C:\CProjects\Forum Help\argsXMacros.exe " -f -c -C -x -y -z
Argument: free: freeing memory....
Argument: compress: doing compression....
Argument: compile: Compiling....
Argument: eXciting: No operation....
Argument: Invalid Argument: Unknown command line argument!
Argument: zero: No operation....



here is the program AFTER the preprocessor gets done with it:
#include <iostream>
using namespace std;

enum ARG_MAP {
    ARG_NONE,
    ARG_c,
    ARG_f,
    ARG_C,
    ARG_z,
    ARG_m,
    ARG_x,
    ARG_COUNT 
};

const char* ARG_VALUES[] = {
    "", 
    "-" "c",
    "-" "f",
    "-" "C",
    "-" "z",
    "-" "m",
    "-" "x",
};



const char* ARG_NAMES[] = {
    "Invalid Argument", 
    "compress",
    "free",
    "compile",
    "zero",
    "milling",
    "eXciting",
};



ARG_MAP getArg(const char* arg) {
    for (int i = 1; i < ARG_COUNT; ++i) {
        if (0 == strcmp(arg, ARG_VALUES[i])) { return (ARG_MAP)i; }
    }
    return ARG_NONE;
}


int main(int argc, char* argv[]) {
    if (argc < 2) {
        cout << "Please add some options" << endl;
        return 1;
    }
    for(int i = 1; i < argc; ++i) {
        ARG_MAP arg = getArg(argv[i]);
        cout << "Argument: " << ARG_NAMES[arg];
        switch(arg) {
            case ARG_c:
                cout << ": doing compression...." << endl;
                break;
            case ARG_f:
                cout << ": freeing memory...." << endl;
                break;
            case ARG_C:
                cout << ": Compiling...." << endl;
                break;
            case ARG_NONE:
                cout << ": Unknown command line argument!" << endl;
                break;
            default:
                cout << ": No operation...." << endl;
                break;
        }
    }
    return 0;
}


Of course I cleaned it up a bit but that should help you understand how it works...

So, let us know how your program comes along.
Was This Post Helpful? 2
  • +
  • -

#10 shadowstep0705  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 48
  • Joined: 26-May 10

Re: string enum comparison

Posted 21 January 2011 - 12:44 PM

View Postjanotte, on 21 January 2011 - 11:25 AM, said:

Okay, what can you do?

Do you know how to compare two strings?
Do you know how to use a command line argument?

What parts of this do you know?

Write the code you do know.


I don't know how to compare two strings, and later I want to convert a std::string to bytes ...
I already posted my current code
Was This Post Helpful? 0
  • +
  • -

#11 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: string enum comparison

Posted 21 January 2011 - 12:51 PM

if you use std::string you can compare strings with '==', if you use c strings(char[],char*) then you can use strcmp(const char*,const char*)
Was This Post Helpful? 0
  • +
  • -

#12 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: string enum comparison

Posted 21 January 2011 - 05:21 PM

View Postshadowstep0705, on 22 January 2011 - 05:44 AM, said:

I don't know how to compare two strings

Okay let's start there.
Ignore the command line argument aspect for the moment and write some code that compares two strings and share it with us.
ishkabible has given you good pointers on how to do that.
Give just that part of the problem a try.
Share your best efforts with us.

View Postshadowstep0705, on 22 January 2011 - 05:44 AM, said:

later I want to convert a std::string to bytes ...

One thing at a time.
If this is for later, do it later.
First work out how to compare two strings.
Was This Post Helpful? 0
  • +
  • -

#13 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: string enum comparison

Posted 21 January 2011 - 06:22 PM

By the way for this functionality:

Quote

I am making a program which works with option flags


there are two things I can suggest once you actually learn to program. On a Linux/Unix/Mac platform or with the GNU toolchain, use getopt. The other is Boost's program_options library, however I'm quite sure that's right over your head at this point.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1