Thread: Trying to compare to strings...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    28

    Trying to compare to strings...

    No matter what the strings are this program always reads them as different strings...

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <string.h>
    
    
    int main(int argc, char *argv[])
    {
    
      char string1[20], string2[20];
      
      cout<<"Input 2 strings. This program will tell you if the 2\n strings are excactly the same  ";
      
      cin.getline(string1, 20);
      cout<<'\n';
      cin.getline(string2, 20);
      
      cout<<"The 2 strings you entered were "<<string1<<" and "<<string2<<'\n';
     
     strcmp(string1, string2);
      
      
          if(string1 == string2)
           {
             cout<<"The two strings you entered are the same";
           }
      
          if(string1 != string2)
           {
            cout<<"The two strings you entered are not the same";
           }
        
      system("PAUSE");	
      return 0;
    }
    Can someone explain why this isnt working and what i need to do to get it to work? Thankyou.
    Last edited by imortal; 05-16-2003 at 10:40 AM.
    ~matt~

  2. #2
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    >strcmp(string1, string2);
    You have to save the return value of strcmp, otherwise the call is useless:
    Code:
    int cmp = strcmp(string1, string2);
    
    if(cmp == 0)
    {
        cout<<"The two strings you entered are the same";
    }
    else
    {
        cout<<"The two strings you entered are not the same";
    }
    Or you can simple use the call to strcmp in the if statement:
    Code:
    if (strcmp(string1, string2) == 0)
    {
    Note that strcmp returns one of three values: 0 if both strings are equal, > 0 if the first string has a higher collective value than the second, and < 0 if the first string has a lower collective value than the second.
    p.s. What the alphabet would look like without q and r.

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787

    Re: Trying to compare to strings...

    Code:
    ...
      cin.getline(string1, 20);
      cout<<'\n';
      cin.getline(string2, 20);
    ...
    here's a suggestion: use cin.get() and include the terminating character (usually '\n') for the string, so the string can be less than 20 characters. That way the person presses enter, the get() stops, and the next get() continues, and is reading on the next line....

    Code:
    ...
     cin.get(string1,20,'\n');
     cin.get(string2,20,'\n');
    ...
    I think that works... if it doesn't work properly, put a cin.ignore(1,'\n') in between the two cin.get().

  4. #4
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    >That way the person presses enter, the get() stops, and the next get() continues, and is reading on the next line....
    Both get and getline remove the newline character from the input stream. The only difference is that getline discards it and get places it in the target string, either way you still move to the next line.
    p.s. What the alphabet would look like without q and r.

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Originally posted by Brighteyes
    either way you still move to the next line.
    I thought with getline() you could read past a return character and include it as part of the string...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    >I thought with getline() you could read past a return character and include it as part of the string...
    You can if you specify the delimiting character as something different:
    Code:
    cin.getline(string, 20, '|'); // Read up to a pipe character
    Now if the stream contains "abc\ndef|", getline will appear to read two lines, placing everything except the pipe character in string.
    p.s. What the alphabet would look like without q and r.

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    that's what I thought... I just use get() to stop at a return character because there's no possibility of it passing that return character... but now that I see that getline() destroys it, I'll probably use that a little more often...

    note: when using get() the terminating character is left in the input stream... http://www.cppreference.com/cppio_details.html#get
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  8. #8
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    > I just use get() to stop at a return character because there's no possibility of it passing that return character...
    You can change the delimiting character for get as well, just like with getline.
    p.s. What the alphabet would look like without q and r.

  9. #9
    Registered User
    Join Date
    Dec 2002
    Posts
    28
    Originally posted by Brighteyes

    Note that strcmp returns one of three values: 0 if both strings are equal, > 0 if the first string has a higher collective value than the second, and < 0 if the first string has a lower collective value than the second.
    what do you mean by collective...
    ~matt~

  10. #10
    Registered User
    Join Date
    Dec 2002
    Posts
    28
    Never mind, i got it

    Thanks guys.
    ~matt~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compare strings problem
    By chelito19 in forum C Programming
    Replies: 2
    Last Post: 04-16-2009, 08:01 PM
  2. how do i compare first letters in two strings?
    By MalickT in forum C Programming
    Replies: 8
    Last Post: 04-20-2008, 05:47 PM
  3. compare strings not working
    By gtriarhos in forum C Programming
    Replies: 7
    Last Post: 09-29-2005, 12:51 PM
  4. using Stacks & Queues to compare Strings
    By eskimo083 in forum C++ Programming
    Replies: 1
    Last Post: 03-09-2003, 05:03 PM
  5. how do i compare strings
    By bart in forum C++ Programming
    Replies: 17
    Last Post: 08-30-2001, 09:17 PM