LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-07-2003, 10:42 PM   #1
crichards
Member
 
Registered: Feb 2003
Location: Arizona
Distribution: Gentoo!
Posts: 124

Rep: Reputation: 15
Command Line Options in C++


I'm trying to learn how to add command line options to a C++ program, and here's what I have (not the entire program, just the relevant parts):

Code:
int main(int argc, char **argv) {
	if (argv[1] == "c")
		std::cout << "Argument 'c'\n";
	else
		std::cout << "No argument\n";
}
I'm wondering why, when I compile it, it doesn't output "Argument 'c'" when I give argv[1] as c (run as ./args c)...

Driving me insane...
 
Old 03-07-2003, 11:29 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
I'm familiar with C, not C++, so this may not apply. I'm going to pretend it's C code though...

The problem is your comparison:
Code:
if (argv[1] == "c")
In essence, you're comapring pointers/addresses and NOT the contents of the string. In C, you'd use the strcmp() function. It would return 0 if the two strings are equal.

Now, I know C++ is fond of function/operator overloading. So the equality test might be mapped to a string comparison function already. However, I doubt that since everything else seems to be ok.

As a side note, you should always check argc before referencing any string arguments. If you reference argv[1] and there were no arguments given, at best, you'll get a segfault. At worst, you'll get garbage that might affect how the program runs. I know, I know, this is just a test program... Just wanted to point that out though...
 
Old 03-08-2003, 12:56 PM   #3
crichards
Member
 
Registered: Feb 2003
Location: Arizona
Distribution: Gentoo!
Posts: 124

Original Poster
Rep: Reputation: 15
OK, I got it to work by using the std::strcmp function, but is there any better C++ alternative? Its in the cstring header, and I can't find an equivalent function in the C++ string header...
 
Old 03-08-2003, 01:01 PM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
You've got me there... I don't know a lot of the standard C++ functions supplied simply because I don't program in it very often. Maybe somebody else watching this thread will know...

Yeah YOU! We know you're out there!
 
Old 03-08-2003, 03:29 PM   #5
sienarot
Member
 
Registered: Mar 2003
Location: Calgary, Alberta, Canada
Distribution: Gentoo 1.4, Debian Woody, Mandrake 8.1
Posts: 43

Rep: Reputation: 15
Is this what you're looking for?

#include<iostream.h>
#include<string.h>

int main(int argc, char *argv[])
{
if(!strcmp(argv[1], "c"))
cout << "Argument 'c'\n";
else
cout << "No argument\n";

return 0;
}
 
Old 03-08-2003, 03:42 PM   #6
sienarot
Member
 
Registered: Mar 2003
Location: Calgary, Alberta, Canada
Distribution: Gentoo 1.4, Debian Woody, Mandrake 8.1
Posts: 43

Rep: Reputation: 15
< oops, double post >
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Command Line Options AUSanders79 Linux - Software 8 03-10-2005 10:24 AM
Help with command line options(ksh) SeT Programming 2 11-29-2004 06:54 PM
gcj command line options m3rc_2003 Programming 0 12-03-2003 05:40 AM
kmail command line options randyasu Linux - Software 10 09-30-2003 03:02 AM
WINE and command line options Power User Linux - Software 2 09-14-2003 06:16 PM


All times are GMT -5. The time now is 07:26 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration