C++ parsing command line arguments & IF statement

jameswhite1979

Senior member
Apr 15, 2005
367
0
0
Hi all,

I am very new to C++ but have been asked by work too figure this out, having tried for a few hours I am turning to you guys hopefully for some help.

I want to execute the appplication with a command line e.g. 'program.exe -FS' and this will change the options displayed in the program (this I have already coded). The code that I have so far is:

=============Code Start==============

#include <iostream>

using namespace std;
int main( int argc, // Number of strings in array argv
char *argv[], // Array of command-line argument strings
char *envp[] ) // Array of environment variable strings
{
int count;
for( count = 1; count < argc; count++ )
cout << "\nDefault Argument: " << argv[1];

if (argv[1] == "-FS") {
cout << "\nWorking" << argv[1];
}

}

=============Code End==============

All the code is working but the If Statement, your help and advice is very welcome :)

Thanks,

James
 

Cogman

Lifer
Sep 19, 2000
10,284
138
106
C++ doesn't support straight forward string (char*) comparisons like you've done there. You'll have to use a function to do that for you.

There are a couple of ways you can do this, one is to create a new string variable, with the string class (Yes, this is confusing, the string class is different from the standard string char*) and store either the compairer or the compairee into it, then do the code as you've written. The other is to use the strncmp function, in which case you'll have to include the cstring header.

As an example of what I'm saying

1st suggestion
Code:
string Val = "-FS";
if (Val == argv[1])
   dothing();

2nd suggestion
Code:
int sCmp = strncmp("-FS", argv[1], 3);
if (sCmp == 0)   // This means the strings were equal
   dostuff();
Hope this helps
 

jameswhite1979

Senior member
Apr 15, 2005
367
0
0
Thanks Cogman for your suggestions but getting an error.

Using suggestion 1 I get this error message on line 'if (Val == argv[1])':

[BCC32 Error] File1.cpp(16): E2094 'operator==' not implemented in type 'string' for arguments of type 'char *'
Full parser context
File1.cpp(8): parsing: int main(int,char * *,char * *)


FYI I having to use Borland C++ Builder (not my choice!).

Cheers, James
 

Cogman

Lifer
Sep 19, 2000
10,284
138
106
Ah, good ole borland, the crappiest C++ compiler out there.

Method 2 should work still, and probably should be what you go for. If however, you still want to use method one, do
Code:
string Val1 = "-FS";
string Val2 = argv[1];
if (Val1 == Val2)
   dostuff();

If that still doesn't work, IDK, borlands really just sucks.
 

jameswhite1979

Senior member
Apr 15, 2005
367
0
0
Hi Cogman,

Tried that and get this error for Option 1:

[BCC32 Error] File1.cpp(18): E2093 'operator==' not implemented in type 'string' for arguments of the same type
Full parser context
File1.cpp(8): parsing: int main(int,char * *,char * *)


Option 2 does compile fine and fully works when using -FS but if no argument is supplied it hangs (couple of seconds) then crashes :(

So option 2 looks more promising but just need it to work when no arguments are passed via the command line (sorry might not have explained what I was trying to do at the start very well).

Thanks,

James
 

Cogman

Lifer
Sep 19, 2000
10,284
138
106
Hi Cogman,

Tried that and get this error for Option 1:

[BCC32 Error] File1.cpp(18): E2093 'operator==' not implemented in type 'string' for arguments of the same type
Full parser context
File1.cpp(8): parsing: int main(int,char * *,char * *)


Option 2 does compile fine and fully works when using -FS but if no argument is supplied it hangs (couple of seconds) then crashes :(

So option 2 looks more promising but just need it to work when no arguments are passed via the command line (sorry might not have explained what I was trying to do at the start very well).

Thanks,

James

Wow, bordland doesn't even have string comparisons? That's incredible.

Ok, your best option is to use the strncmp function. For the "Crashs when no argument exists" Problem, you'll need to check argc before doing the comparison (it should be greater than or equal to 2). right now, strncmp is trying to dereference a garbage pointer, and is throwing a fit over it.

One thing you might check with option one (though, I don't have my hopes up at this time) is if #include <string> will fix the problems that occurred earlier. It is possible that they didn't include the full definition for the string from wherever it is that you are getting it from.
 
Last edited:

jameswhite1979

Senior member
Apr 15, 2005
367
0
0
Hi would the code to check that argc => 2 be this:?

if (argc >= 2 ) {
blahblah;
}

I am so hating Borland now!
 

jameswhite1979

Senior member
Apr 15, 2005
367
0
0
Ok thanks to Cogman's help I have now got it all working - For others here is the code:


//////////////////////////////Code Start//////////////////////////////
#include <iostream>

using namespace std;
int main( int argc, // Number of strings in array argv
char *argv[], // Array of command-line argument strings
char *envp[] ) // Array of environment variable strings
{
int count;

for( count = 1; count < argc; count++ )

if (argc >=2)
{

int sCmp = strncmp("-FS", argv[1], 3);

if (sCmp == 0)
{
cout << "\n\n\n\nArgument Passed: " << argv[1];
}
else
{
cout << "\n\n\n\No Argument passed";
}
}
}

//////////////////////////////Code End//////////////////////////////
 

Kirby

Lifer
Apr 10, 2006
12,028
2
0
lol borland. i'd find someone to bitch to about that. :D

good luck with your endeavors, you may need it. :p
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
lol borland. i'd find someone to bitch to about that. :D

good luck with your endeavors, you may need it. :p

Sad, isn't it? I still have my Borland C++ 5.1 CDs. It used to be the shiznit back in the day, before MS raided their tools team with advanced stock option weapons.
 

Kirby

Lifer
Apr 10, 2006
12,028
2
0
Sad, isn't it? I still have my Borland C++ 5.1 CDs. It used to be the shiznit back in the day, before MS raided their tools team with advanced stock option weapons.

Man, I was a pro at BGI. :awe: