Argc/argv problem

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
TheTask1337
Posts: 21
Joined: October 30th, 2014, 9:10 pm

Argc/argv problem

Post by TheTask1337 » November 2nd, 2014, 2:09 pm

Hello Guys I am beginner at C, but I need to solve this problem with argc/argv. I am working with Linux in this example.
How does that actually work?
Here is my code:

Code: Select all

int main( int argc,char* argv[] )
{
    switch( argc )
{
case 2:
    if( argv[1] == "--help" )
    {
        printf( " This is program's help page!\n\n" );           
    }
    else
    {
    printf( "Invaild argument\n" );
    }
    break;
    };
}
When I compile that (standard $gcc -o file file.c) and run (./file --help ) I get the "Invadil Argument" - the else case thing. Why?
Last edited by TheTask1337 on November 3rd, 2014, 1:01 pm, edited 1 time in total.

cameron
Posts: 794
Joined: June 26th, 2012, 5:38 pm
Location: USA

Re: Argc/argv problem

Post by cameron » November 2nd, 2014, 6:16 pm

Try getting rid of ; at end of switch.
Computer too slow? Consider running a VM on your toaster.

User avatar
BurakCanik
Posts: 250
Joined: February 8th, 2014, 9:16 pm
Location: Istanbul, Turkey

Re: Argc/argv problem

Post by BurakCanik » November 2nd, 2014, 7:57 pm

Don't think that's the issue here because if I recall correctly it's up to you to put that semicolon there. Compiler allows both syntax there if I'm not mistaken.
If real is what you can feel, smell, taste and see, then 'real' is simply electrical signals interpreted by your brain" - Morpheus

TheTask1337
Posts: 21
Joined: October 30th, 2014, 9:10 pm

Re: Argc/argv problem

Post by TheTask1337 » November 2nd, 2014, 8:22 pm

Yeah, compiler doesn's say anything when the semicolon is there or isn't. But still the problem hasn't been solved by that.

Natories
Posts: 107
Joined: June 21st, 2012, 7:06 am
Location: USA
Contact:

Re: Argc/argv problem

Post by Natories » November 3rd, 2014, 9:35 am

What about a semi-colon after the first printf statement? Could this be a problem?

Also, what does the second to the last "};" used for?

Natories

TheTask1337
Posts: 21
Joined: October 30th, 2014, 9:10 pm

Re: Argc/argv problem

Post by TheTask1337 » November 3rd, 2014, 1:01 pm

Natories wrote:What about a semi-colon after the first printf statement? Could this be a problem?

Also, what does the second to the last "};" used for?

Natories
Nah, somehow I managed not to copy that semicolon...wut. Gonna put it there now, thanks. But that is not the problem, because compiler would show an error (I had that semicolon in my code). The last "};" is the ending of switch.

Natories
Posts: 107
Joined: June 21st, 2012, 7:06 am
Location: USA
Contact:

Re: Argc/argv problem

Post by Natories » November 4th, 2014, 5:57 am

Gotcha, didn't know if it was just something specific to Linux or otherwise! Good luck!

Natories

User avatar
LuisR14
Posts: 1248
Joined: May 23rd, 2013, 3:52 pm
Location: USA
Contact:

Re: Argc/argv problem

Post by LuisR14 » November 9th, 2014, 7:03 pm

TheTask1337 wrote:

Code: Select all

    if( argv[1] == "--help" )
that wouldn't be the right way to compare strings, you'd instead have to use strcmp()
always available, always on, about ~10 years c/c++, java[script], win32/directx api, [x]html/css/php/some asp/sql experience. (all self taught)
Knows English, Spanish and Japanese.
[url=irc://irc.freenode.net/#pchili]irc://irc.freenode.net/#pchili[/url] [url=irc://luisr14.no-ip.org/#pchili]alt[/url] -- join up if ever want real-time help or to just chat :mrgreen: --

TheTask1337
Posts: 21
Joined: October 30th, 2014, 9:10 pm

Re: Argc/argv problem

Post by TheTask1337 » November 13th, 2014, 2:47 pm

LuisR14 wrote:
TheTask1337 wrote:

Code: Select all

    if( argv[1] == "--help" )
that wouldn't be the right way to compare strings, you'd instead have to use strcmp()
yeah, later on I figured out this way. But thanks anyway. Also, this works, too.

Code: Select all

if( ( string )argv[1] == "--help" )

Post Reply