[SOLVED] char* compare to string, c++

Ive been working on a program for my personal use and I getting to the point where I need command line arguments. For the life of me I can't get the compare to return true. Here is a bit of what I've been trying. If someone could show me or point me to somewhere that I could find how to do this, please.
int main(int argc, char* argv[]){
std::cout<< argv[1];
std::cout<< argc;
std::cout<<(argv[1]=="-h");
That outputs
-h
2
0
I dont understand why it's false, I have even tried (argv[1] == "-h\0") to see if adding a null terminator would help.
Thank you in advance for the help.
Last edited by tpolich (2009-08-18 19:05:06)

You get the wrong result because operator "==" compares addresses in that case. Note that argv[1] is not std::string type but char*. You may create std::string object from argv[1] and then use "==" operator:
std::cout << (std::string(argv[1]) == "-h");
provided that your c++ compiler will treat "-h" as std::string.
You can also try strcmp() function which operates on C strings ( char *). It returns 0 if C strings are equal so:
if(strcmp(argv[1], "-h") == 0) {
std::cout << "true";

Similar Messages

  • Boolean, comparing user string & user char

    I'm missing something. i'm trying to compare a string, from user input, to a char, from user input. Ex. string, char: "sssss" "s". Solving for does string "sssss" contain only char "s" ? In this case, should return true. In "ssss sd" "s" SOP should retun false false. I'm having trouble writing code so that string "s", however many, = char "s". Probably confusing, I'm sorry. Here's what I have:
    //     StringInput.java          
    public class StringInput
         public static void main(String[] args)
         String s = args[0];          //get the String from the user
         char c = args[1].charAt(0);     //get the char from the user
         int i = 0;
         boolean answer = true;{
         while (answer && (i < s.length () - 1)) {
    //char c = s.charAt (i);
         //if (! (((c >= '0') && (c <= s.length)) || (c == ' ')))
         answer = false; }}
         String trim = s.trim();
         s = trim;
         String replace = s.replace('a', 's');
         int length = s.length();
         System.out.println (trim);
         System.out.println (replace);
         System.out.println (length);
    System.out.println("The String " + s + " is composed " + " entirely of the char " + c + ": " + answer);                          

    This might work
    class ScanInput
         public static void main(String[] args)
              String s = args[0]; //get the String from the user
              char c = args[1].charAt(0); //get the char from the user
              boolean answer = false;
              int i =0;
              while ( (! answer) && ( i < s.length( ) -  1 )  )
                        if ( c != s.charAt( i ) )
                                  answer = true;
                        i++;
              if ( answer)
                        System.out.println(" The input string " + s + " contains other characters besides " + c );
              else
                        System.out.println(" The input string " + s + " contains only character " + c );
    }

  • Compare individual char of 2 string

    Hi
    I need to compare the each individual char of 2 string.
    String a = "Ae12";
    String b = "aE12";
    // I tried the followings:
    // a
    String c = a.substring(0,0);
    String d = a.substring(1,1);
    String e = a.substring(2,2);
    String f = a.substring(3,3);
    // b
    String c1 = b.substring(0,0);
    String d1 = b.substring(1,1);
    String e1 = b.substring(2,2);
    String f1 = b.substring(3,3);
    if(c.equalsIgnoreCase(c1)&&d.equalsIgnoreCase(d1)&&e.equalsIgnoreCase(e1)&&f.equalsIgnoreCase(f1)) // Cannot be compared
    // and I tried the followings:
    // a
    char c = a.charAt(0);
    char d = a.charAt(1);
    char e = a.charAt(2);
    char f = a.charAt(3);
    // b
    char c1 = b.charAt(0);
    char d1 = b.charAt(1);
    char e1 = b.charAt(2);
    char f1 = b.charAt(3);
    if(c==c1&&d==d1&&e==e1&&f==f1) // cannot be compared.
    Both cases cannot be compared. How can I fix it?
    Because I want to compare those 2 strings without case sensitive and numbers.
    Thanks

    techissue2008 wrote:
    String a = "Ae12";
    String b = "aE12";
    Then, are "Ae12" and "aE12" the same when ignore case?
    If so, the Strings are actually the same.No, the Strings are not the same, if by same, you mean the "same" Object, or the "same" value when case is not ignored, but they are the "same" value when case is ignored.
    This prints true (try it yourself), as it should
    public class Test {
      public static void main (String[] args) {
        System.out.println("aE12".equalsIgnoreCase("Ae12"));
    }They are still two distinct instances with two distinct values however, so where, exactly, is your problem?

  • Comparing a char to a string value

    hello.
    i would like to take a string, take the first value, see if it is equal to something, and based on that condition, proceed through some conditional statements.
    i tried this, but doesnt work.
    String path;
    char c = path.charAt( 0 );
    if ( c.equals("/") ) {
    Blah
    ive also tried to cast the char as a String and it didnt work.
    if ( ( (String)c ).equals("/") ) {i originally tried
    any ideas?
    thanks.
    J                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

    >
    any ideas?I always liked...
      switch(path.charAt(0))
         case '/': ....

  • Compare two strings in a formula

    Hi,
    I'd like to know if there's a way to compare two strings in a formula ?
    I have this consition:
    IF(@SUBSTRING(@NAME(@CURRMBR(TIME_FROM)),5)==@NAME(@CURRMBR(TIME)))
    But the == operator can only compare two data.
    Thanks,
    Cyril

    Hi,
    thank you both for your answers.
    When I figured that the == operator was for data only, I didn't even try to compare member names.
    I finally used the solution that Cameron suggested in another conversation. I created a date measure, and loaded some data since the beginning of my time to the end.
    It starts in 1974 (annually), then becomes monthly data, and then daily data from 2009, so it's not so many blocks to br created. In the end I have the same value for "from 2008"/"from Apr 30", and I can easily compare my dates.
    Note that I could have also used the @TODATE function, which turns a date into a number, but the format of date is limited and doesn't fit the format of my member names, so I got lasy... and I'm not sure my users would have apreciated that ;)
    Thanks, this post is solved.
    Cyril

  • Finding last char in the string

    Hi
    i need to find out whether the last char of a string is '/' (forward slash). please let me know if there's a FM to do that. Other post me the logic for this ASAP.
    thanks

    len = strlen(char).
    len = len - 1.
    if char+len(1) = '/'.
      *write ur code
    endif.
    chk this code
    REPORT ychatest.
    DATA : str(5) VALUE '123/',
           len TYPE i.
    len = STRLEN( str ).
    len = len - 1.
    IF str+len(1) = '/'.
      WRITE : '/ found'.
    ENDIF.
    <b>Reward points if helpful and close thrad if solved</b>
    Message was edited by: Chandrasekhar Jagarlamudi

  • How to compare two strings whether both are equal while ignoring the difference in special characters (example: & vs & and many others)?

    I attempted to compare two strings whether they are equal or not. They should return true if both are equal.
    One string is based on Taxonomy's Term (i.e. Term.Name) whereas other string is based on String object.
    The problem is that both strings which seem equal return false instead of true. Both string values have different special characters though their special characters are & and &
    Snapshot of different design & same symbols:
    Is it due to different culture or language?
    How to compare two strings whether both are equal while ignoring the difference in special characters (& vs &)?

    Hi Jerioon,
    If you have a list of possible ambiguous characters the job is going to be easy and if (& vs &) are the only charracters in concern awesome.
    You can use the below solution.
    Before comparing pass the variables through a replace function to standarize the char set.
    $Var = Replace($Var,"&","&")
    This is going to make sure you don't end up with ambiguous characters failing the comparison and all the char are "&" in this case.
    Similar technique is used to ignore Character Cases 'a' vs. 'A'
    Regards,
    Satyajit
    Please “Vote As Helpful”
    if you find my contribution useful or “Mark As Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.

  • How to take last 5 char from a string

    Hi All
    I have a requirement,
    base on some comp i have to populate another field.
    in this case i have compare last 3 char of a string 
    example if total string say asasdffrx i have to take last three char here it is frx.
    always i have to last 3 char.
    i can use +6(3) but it wont work every time because string lenght is not fix for all vaue.
    thanks

    Hi,
    A.H.P sample is ok but insert additional check for strigs shorter than 3 (to avoid negative offset):
    data : n TYPE I.
    n = STRLEN( zfield ) - 3.
    if n >= 0.
    result = zfield+n(3).
    else.
    result = zfield.
    endif.
    Krzys

  • Comparing CAN strings to know if i have the right one.

    I am trying to write a program to test a product that we produce using CAN.  How would I go about seeing if I have a certain CAN string message to to know that our alarm is turned on, on the product?  Is there some way to compare the incomming strings, and have it pass some bolean value when it does?  Thanks, Fred
    Solved!
    Go to Solution.

    Hi Fred420,
    you can use the simple compare function to compare two strings. If you have a list of strings, then you can store them in an array and use the Search 1D Array function. Two find a part of a string in another string, you can use the search and replace function.
    Hope it helps.
    Mike

  • How to delete the last char in a String?

    i want to delete the last char in a String, but i don't want to convert the String to a StringBuffer or an array, who knows how to do?

    Try it in this way
    String MyString = "ABCDEF";
    MyString = MyString.substring(0,MyString.length()-1);

  • How to Compare two strings in PL/SQL

    Hi All,
    I need to compare two strings whether they are equal or not in PL/SQL.Is there any function to comparing the strings.

    Yes, the = sign.
    Are you after something like:
    IF v_string1 = v_string2 THEN
    ELSE
    END IF;?
    Edited by: Boneist on 27-Aug-2009 11:41

  • Compare two strings in an array

    Hello!
    Please feel free to give me hints, but do not give me any code.
    Ok, here is what I am trying to do, I whant to compare my input string whith strings allready stored in the array. If my input string equals any of the allready excisting strings, a error wil show upp, otherwise store the new string in next avalibal position. I do not seem to get the method right for comparing the strings. here is the method I have written so far, please take a look and give me a hint, but no code. :)
    //Check if a user already excists     
    public void compareNames (People in_array [], String in_name) {
              for (int i=0; i<value.peopleCount; i++) {
                   if (in_name.equals(in_array.getPeople())) {
                   System.out.print("The user already excist, please chose another name");
    }value.peopleCount is an global count value for people arays.
    getPeople get the name from the people class.
    Martin                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

    A couple notes here.
    The name compareNames() is misleading if it is going to do what you described. A comparison will generally not have side-effects like outputting error messages to the console or adding new items to an array. It would be better if you called the method addIfNew(), and returned a boolean indicating whether the name was new or not. The caller would then be responsible for displaying an error message if the method returned false.
    I also suggest you use a List such as ArrayList instead of an array, otherwise you will have to resize and copy your array every time you add something to it that exceeds the array size, and will allow you to do away with the global peopleCount.

  • Compare two string in different line in textarea

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class TextCounterPanel extends JPanel {
    private JTextArea textInput;
    private JLabel lineCountLabel;
                        public TextCounterPanel() {
                        setBackground(Color.DARK_GRAY);
                        textInput = new JTextArea();
                        textInput.setBackground(Color.WHITE);
                        JPanel south = new JPanel();
                        south.setBackground(Color.DARK_GRAY);
                        south.setLayout( new GridLayout(4,1,2,2) );
                             JButton countButton = new JButton("Process the Text");
                             countButton.addActionListener( new ActionListener() {
                                  public void actionPerformed(ActionEvent evt) {
                                       processInput();
                             south.add(countButton);
                        lineCountLabel = new JLabel(" Number of lines:");
                        lineCountLabel.setBackground(Color.WHITE);
                        lineCountLabel.setForeground(Color.BLUE);
                        lineCountLabel.setOpaque(true);
                        south.add(lineCountLabel);
                        setLayout( new BorderLayout(2,2) );
                        setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
                        JScrollPane scroller = new JScrollPane( textInput );
                        add(scroller, BorderLayout.CENTER);
                        add(south, BorderLayout.SOUTH);
    public void processInput() {
    String text,vin; // The user's input from the text area.
    text = textInput.getText();
    vin =text.substring(25,42);
    lineCountLabel.setText(" vin: " + vin);
    } // end class TextCounterPanel
    How can I compare two string in different line in text area

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class TextCounterPanel extends JPanel {
    private JTextArea textInput;
    private JLabel lineCountLabel;
    public TextCounterPanel() {
    setBackground(Color.DARK_GRAY);
    textInput = new JTextArea();
    textInput.setBackground(Color.WHITE);
    JPanel south = new JPanel();
    south.setBackground(Color.DARK_GRAY);
    south.setLayout( new GridLayout(4,1,2,2) );
    JButton countButton = new JButton("Process the Text");
    countButton.addActionListener( new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
    processInput();
    south.add(countButton);
    lineCountLabel = new JLabel(" Number of lines:");
    lineCountLabel.setBackground(Color.WHITE);
    lineCountLabel.setForeground(Color.BLUE);
    lineCountLabel.setOpaque(true);
    south.add(lineCountLabel);
    setLayout( new BorderLayout(2,2) );
    setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
    JScrollPane scroller = new JScrollPane( textInput );
    add(scroller, BorderLayout.CENTER);
    add(south, BorderLayout.SOUTH);
    public void processInput() {
    String text,vin; // The user's input from the text area.
    text = textInput.getText();
    vin =text.substring(25,42);
    lineCountLabel.setText(" vin: " + vin);
    } // end class TextCounterPanel

  • Comparing  NVL strings in IF statement

    ORA-06550: line 22, column 12: PLS-00412: list of values not allowed as argument to this function or procedure ORA-06550: line 22, column 1: PL/SQL: Statement ignored
    if     nvl((lower(v_address),'null') not like (lower(:ADDRESS)))

    Hi,
    Lucy Discover wrote:
    Whats the valid syntax to compare 2 strings . I want to compare it even if the first string is null.This is the correct syntax
    if     nvl (lower (v_address), 'null') not like lower (:ADDRESS)assuming V_adress and and :address are strings (and not some kind of collection, even if made up of strings).
    I hope this answers your question.
    If not, post a complete procedure or block, a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables, so that people can re-create the probnlem and test their ideas. Also post the results you want from that data.
    Explain, using specific examples, how you get those results from that data.
    Always say which version of Oracle you're using.

  • Comparing 2 strings in if statement

    Hi All,
    I want to compare two strings in if statement. When I use "==" then its throwing error that expected number and got string. Can anyone please let me know how can we compare two strings in an IF statement?
    Thanks in advance

    The OP doesn't state what release he's on.
    The functions @LIKE and @MBRCOMPARE are 11.1.2 and up only.
    OP, if you're at a lower release, I've faced this problem as well in 11.1.1.3. Here's an older thread that detailed how I got around it for a client:
    Re: How to compare two strings in calc script?
    It really wasn't hard to do and the performance, in the limited context I was using it in, was fine.
    Regards,
    Cameron Lackpour

Maybe you are looking for

  • Web Start no longer working with JavaFX?

    As of 7.25 (at least), whenever I create a JavaFX project in Netbeans (using 64-bit Windows JDK and Netbeans), trying to launch it with the JNLP file that Netbeans automatically generates results in the following: java.lang.NullPointerException     a

  • My ipod is stuck on a white screen after i tried to update IOS and turns on and off by itself :/

    ok so last night im textin my sis with this app and i thought she was done texting me so i decided to update my ipod to IOS 6 and it took FOREVER just to download it idk why but anyways it said i needed a power source or a battery level over 50% so i

  • Connecting Enterprise Directly to Pervasive

    Good Afternoon, I'm working as a remote consultant with a firm that wants me to find out about running pervasive direct from the BO Server.  They are migrating from simple client server with an Enterprise Server to Data Warehousing.  Right now though

  • ME23N - Read Last Purchase Order for Display

    Hi, We are upgrading from 4.6C to ECC6 and one of the problems the users have identified is that when displaying the purchase order with transactions ME22N or ME23N the last purchase order that was worked on is not always the one that is displayed. T

  • SAP report to display mat number, quantity on hand, price, material group.

    Hi, I wish to have report to display info during month end: mat number, quantity on hand, price, material group. Please suggest me list of report to retrieve these information. Thanks