views:

165

answers:

3

I am new to C++ and I would like to know how can I do a simple program where some functions are available.

For example:

mypg.exe function1 string1
mypg.exe function1 textfile1

Takes the text from the str/file and shows or prints.

And then:

mypg.exe function2 string1
mypg.exe function2 textfile1

Uses the text for a different thing... and so on.

Any manual/tutorial or help? What should I learn to be able to do this?

Thanks a lot :)

+1  A: 
int main(int argc, char * argv[])
{  
   // argc - num of command line arguments
   // argv - arguments
}
Alexey Malistov
Not really a good example for a new guy...
Andres Jaan Tack
+8  A: 
void DifferentThing(char *s)
{
    printf("DifferentThing %s\n",s);
}

int main (int argc, char * const argv[])
{
    if (argc > 2)
    {
        if (strcmp(argv[1], "function1") == 0)
            printf("%s\n",argv[2]);
        else if (strcmp(argv[1], "function2") == 0)
            DifferentThing(argv[2]);
    }
    return 0;
}

Update: The above is not really C++, more like C. This is better

#include <iostream>
#include <string>

void DifferentThing(const std::string &s)
{
    std::cout << "DifferentThing " << s << std::endl;
}

int main (int argc, char * const argv[])
{
    if (argc > 2)
    {
        std::string param1(argv[1]);
        std::string param2(argv[2]);

        if (param1 == "function1")
            std::cout << param2 << std::endl;
        else if (param1 == "function2")
            DifferentThing(param2);
    }
    return 0;
}
David Sykes
I think that this can be useful :) Thanks!What about doing that different thing, depending on the second argument (if it's a file, extracts the text and then does the same that will do with the string (e.g. print))
andofor
@ofme: note that this is C, not C++. If you're learning C++, it would be more useful to learn about the `string` class than C functions like `strcmp`.
Mike Seymour
@Mike you beat me to my update by 60 seconds!
David Sykes
@ofme I think those are better addressed in a separate search/question
David Sykes
ok, thanks again! If the string is more than one word, I have to use "for example this string", I guess. Is it limited the length of the string?
andofor
@ofme yes, and it depends on the platform, though any limit is likely to be large
David Sykes
+3  A: 

In C++, the program does not have any idea what it's functions are called; That is to say it isn't possible to, (when given a function name as a string) link it back to the original function.

However if you make this connection manually, this can be achieved.

#include <iostream>//for cout
#include <string.h>//for strcmp

using namespace std;

int main(int argc,char **argv){

    if(argc<3){
        cout << "not enough parameters\n";
        return 0;
    }

    if(strcmp(argv[1],"fuction1") == 0){//strcmp() returns 0 if the two strings match
        //call function1 and do something with argv[2]
    }else if(strcmp(argv[1],"function2") == 0){
        //call function2 and do something with argv[2]
    }

    return 0;
}

Although you would still have to decide on whether to treat argv[2] as a plain "string" or as a filename.

sigint
!strcmp is going to be confusing to a beginner
David Sykes
@David Sykes this is true. I've been doing a lot of C lately, so I didn't even notice. Thanks and edited.
sigint