Sign in to follow this  
prophete

Passing integer argument to main?

Recommended Posts

Well, without seeing your code, we can only post correct code for you to compare:


#include <iostream>
#include <sstream>

int main(int argc,const char **argv)
{
++argv; // skip exe path

if(*argv)
{
std::istringstream is(*argv);

int i; is >> i;

std::cout << "Integer argument was - " << i << std::endl;
}
}


I'm guessing from your question that perhaps you didn't realise that the arguments to main are ALWAYS character array strings. If you want to convert one to an int, you need to do the conversion yourself.

With the method above, you could query the fail() method of the stringstream after the is >> i operation to ensure that a valid and convertible number was given.

HTH

Share this post


Link to post
Share on other sites
That's because when you want to start your application you type something like the following string (or Windows/Linux does it for you, through a shortcut):

C:>foo.exe -f file.foo -d 123

Then the OS will execute your program foo.exe. What gets passed to your main function is the entire line you type minus the executable name.
So argv equals: -f file.foo -d 123

Answer, how can you pass a integer to the main function? You don't, you will need to cast it a parameter to the appropiate type. So only if you absolutely sure a parameter is an integer you should cast it.

Hope that helps.

Starik

edit: what EasilyConfused said

Share this post


Link to post
Share on other sites
Quote:
Original post by StarikKalachnikov
What gets passed to your main function is the entire line you type minus the executable name.
So argv equals: -f file.foo -d 123


Sorry to nitpick but I believe the standard dictates that the first argument will always be the name of the exe. Whether or not this is the full path to the exe is implementation dependant, however.

Share this post


Link to post
Share on other sites
Quote:
Original post by EasilyConfused
Quote:
Original post by StarikKalachnikov
What gets passed to your main function is the entire line you type minus the executable name.
So argv equals: -f file.foo -d 123


Sorry to nitpick but I believe the standard dictates that the first argument will always be the name of the exe. Whether or not this is the full path to the exe is implementation dependant, however.


That's not nitpicking, that's making an important correction (to avoid confusion as to why argv[0] wouldn't be "-f" in that case).

A nitpick would be pointing out that, technically, the implementation is free to make the first argument "\0" or some such. I forget the details, so I can't properly nitpick your not nitpick, unfortunately.

...then again I suppose I'm nitpicking by pointing out you weren't nitpicking, so consider yourself nitpicked! Sorry.

Share this post


Link to post
Share on other sites
Quote:
Original post by EasilyConfused
Quote:
Original post by StarikKalachnikov
What gets passed to your main function is the entire line you type minus the executable name.
So argv equals: -f file.foo -d 123


Sorry to nitpick but I believe the standard dictates that the first argument will always be the name of the exe. Whether or not this is the full path to the exe is implementation dependant, however.


I don't want to pick anyone's nits (um, I've had enough experience at that with my kids bringing their unwanted guest home from school) but nether the C nor the C++ standard specify the contents of argv (which is in fact of type char*[], not char**). In fact, they don't even require an argv argument at all, but allow the arguments to main() to be system defined. The POSIX standard, for example, specifies the arguments to main() as main(int argc, char* argv[], char*envp[]).

On a number of Important Platforms argv[0] has traditionally been an empty string, so don't count on it being useful in any way. By convention, the command-line arguments on a Unix system are passed in argv[N] where N is in {1, ...}. MS Windows, based on MS-DOS, which was based on CP/M, which was loosely based on Unix, follows the same convention.

--smw

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this