LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 08-30-2006, 02:41 PM   #1
websinger
Member
 
Registered: Jul 2003
Location: Virginia
Distribution: LMDE-2 3.11.2amd64
Posts: 35

Rep: Reputation: 15
C++ matching variable comparisions


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 02:57 PM.
 
Old 08-30-2006, 02:49 PM   #2
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
use strcmp to compare your strings
Code:
if(!strcmp(x, "xy"))  // strcmp returns 0 on match
  cout << "it matches !" << endl;
also

USE CODE TAGS!
 
Old 08-30-2006, 02:54 PM   #3
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
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!
 
  


Reply

Tags
c++, programing



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 9 06-30-2016 05:30 PM
setting a variable variable in a script... this works, but could it be more elegant? pwc101 Programming 3 08-18-2006 11:23 AM
Counting matching file extentions variable Fegero Programming 1 11-17-2003 12:34 PM
Palm Desktop Clone comparisions? oswald21 Linux - Software 2 06-05-2003 06:31 AM
Comparisions theneoprotocol *BSD 10 04-27-2002 10:22 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:04 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration