• 10
• 12
• 12
• 14
• 15

Passing integer argument to main?

This topic is 4038 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi, i'm trying to pass an integer argument to main, but it's not working, not matter what i set argv's type to (int, char, char*..)

Share on other sites
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 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 on other sites
Quote:
 Original post by StarikKalachnikovWhat 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 on other sites
Quote:
Original post by EasilyConfused
Quote:
 Original post by StarikKalachnikovWhat 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 on other sites
I wish I could think of a way to nitpick your nitpicking of my non-nitpicking, but unfortuantely I'm sure you are completely correct. [smile]

Share on other sites
Moved to For Beginners.

Share on other sites
Quote:
Original post by EasilyConfused
Quote:
 Original post by StarikKalachnikovWhat 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