tags:

views:

119

answers:

4

Okay, for a homework assignment my professor wants us to write a program in C++ that converts from miles to km. I did all that, the program runs. However, he has this special way of calling the program:

The program's usage options are based on the NAME of the BINARY FILE. If the program name is 'km2miles', the program interprets the command line argument as a kilometer value to convert to miles. If the name is 'miles2km', then it interprets as miles being converted to km. Since the first command line argument, argv[0] is always the program's name, you can use its value to decide which function to call.

I only have 3 files in this project (he tells us to ONLY have these 3): convert.cpp distance.cpp distance.h

Distance .h and .cpp have the different functions to convert Mi to Km and Vice Versa, the convert.cpp has the main function. However, the only way I know how to call this program (after compiling it) is to say:

./convert 10

Where 10 is the number to convert. He says it should be called like this:

$ km2miles 100

and

$ miles2km 60

I have no idea how to get the program to act differently by having a different name... especially when that name doesn't even run the program! Help would be appreciated.

+1  A: 

Well, he gave you one clue with the argv[0] thing.

Did you perhaps discuss symbolic links at some point in your class?

Difficult for me to give more hints without actually giving away the answer.

Jim Mischel
I don't recall talking about symbolic links at all in class, just details about the make file and compiling. I understand that argv[0] is the program name, but I need to call the program with that, so:"./convert 10", in the terminal. In which case the name is Convert. If I type: "./km2miles 10" it simply complains about there not being a name for the program.
Wayfarer
Just do `cp convert km2miles;./km2miles 10`. Copying files is easier to understand (you yourself have probably done it thousands of times) so start with that. Then read up on links and the `ln` command (sort of like shortcuts in the Windows world).
slebetman
+2  A: 

The instructions already tell you how:

Since the first command line argument, argv[0] is always the program's name, you can use its value to decide which function to call.

especially when that name doesn't even run the program!

If you're using gcc, by default it generates a binary named a.out, but you can rename it to be whatever you want. (Or you can specify the name of the output file via the -o command-line option.)

jamesdlin
hmmm.... Perhaps he did mean to rename it....
Wayfarer
A: 

If you don't want to recompile the same code into 2 different executable files then you may need to use a symbolic link: http://en.wikipedia.org/wiki/Symbolic_link

shuttle87
+4  A: 

You can:

  • specify a name when you build it, and build it twice
  • on Windows: copy convert miles2kms; copy convert kms2miles
  • on UNIX/Linux: cp convert miles2kms; cp convert kms2miles
  • on UNIX/Linx (better): make a link or symbolic link: ln -s convert miles2kms; ln -s convert kms2miles.

Inside your program, you should be doing something like:

#include <string>
#include <iostream>

int main(int argc, const char* argv[])
{
    std::string program_name = argv[0];

    if (argc != 2) {
        std::cerr << "usage: " << program_name << " <value>\n";
        return 0;
    }
    if (/* TODO: what would go here? */)
        ...
    else
        ...
}
Tony
Why not `std::string program_name(argv[0]);`? Oh, and for an *extreme* example of this technique. look up [busybox](http://www.busybox.net/).
Mike DeSimone
Hi Mike... just a stylistic choice. C++ guarantees they're equivalent in this case, but I find the assignment notation more readable. Great example in busybox.
Tony