LinuxQuestions.org
Visit Jeremy's Blog.
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

Tags used in this thread
Popular LQ Tags ,

Reply
 
Thread Tools
Old 08-30-2006, 03:41 PM   #1
websinger
Member
 
Registered: Jul 2003
Location: Virginia
Distribution: Debian -sid
Posts: 34
Thanked: 0
C++ matching variable comparisions


[Log in to get rid of this advertisement]
Hi Im tring to do a simple match here , and that which should match
isn't . it's a pretty simple program. but I dont see the problem

anyone have an inkling?

thanks

Jeff
Code:
~$ cat argtest.c++
#include<iostream>
using namespace std;

int main(int argc, char * argv[])
{
    char *x;
    x=argv[1];

    cout << " the value of X is "<<x<<endl;

    if( x == "xy" )cout<< "it matches !"<<endl;
    else cout<<" no match:"<< endl;
}
~$ g++ argtest.c++ -oargtest
~$ ./argtest xy
the value of X is xy
no match:

other useful (?) info
:~$ g++ -v
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --program-suffix=-4.1 --enable-__cxa_atexit --enable-clocale=gnu --enable-libstdcxx-debug --enable-mpfr --enable-checking=release x86_64-linux-gnu
Thread model: posix
gcc version 4.1.2 20060814 (prerelease) (Debian 4.1.1-11)

Last edited by websinger; 08-30-2006 at 03:57 PM..
websinger is offline  
Tag This Post ,
Reply With Quote
Old 08-30-2006, 03:49 PM   #2
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065
Thanked: 0
use strcmp to compare your strings
Code:
if(!strcmp(x, "xy"))  // strcmp returns 0 on match
  cout << "it matches !" << endl;
also

USE CODE TAGS!
xhi is offline     Reply With Quote
Old 08-30-2006, 03:54 PM   #3
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065
Thanked: 0
or use std::string

Code:
string argtwo;
if(argc > 1)
    argtwo = argv[1];
if(argtwo == "xy")
  cout << "it matches !" << endl;
also

see how neatly that example is formatted, with spacing and all. that is because i

USE CODE TAGS!
xhi is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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
Find/grep command to find matching files, print filename, then print matching content stefanlasiewski Programming 7 01-06-2008 03:10 AM
setting a variable variable in a script... this works, but could it be more elegant? pwc101 Programming 3 08-18-2006 12:23 PM
Counting matching file extentions variable Fegero Programming 1 11-17-2003 01:34 PM
Palm Desktop Clone comparisions? oswald21 Linux - Software 2 06-05-2003 07:31 AM
Comparisions theneoprotocol *BSD 10 04-27-2002 11:22 AM


All times are GMT -5. The time now is 01:37 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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration