Thread: Compare Two Strings?

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    167

    Compare Two Strings?

    Code:
    	ifstream input_file("input.txt");
    	string line;
    	getline( input_file, line, '\n' );
    line now has the first line of the file: "C++Draw Object File V1.0"

    Now I do:

    Code:
    	cout << line.compare("C++Draw Object File V1.0") << "\n";
    	cout << line.compare("C++Draw Object Fidle V1.0") << "\n";
    Notice the misspelling on the word File in line 2.

    The first line prints: "1"
    The second line prints: "1"

    Wtf?? How can they be the same??

    How do I compare strings correctly?
    Last edited by Paul22000; 05-24-2008 at 03:27 PM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Compare returns 0 if the strings match correctly. If you read the documentation for std::string.compare, you would understand more.
    The more proper way of comparing strings is simply
    Code:
    	cout << (line == "C++Draw Object File V1.0") << "\n";
    	cout << (line == "C++Draw Object Fidle V1.0") << "\n";
    Last edited by Elysia; 05-24-2008 at 03:49 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Are you sure? I'm getting:

    main.cpp:39: error: invalid operands of types ‘const char [25]’ and ‘const char [2]’ to binary ‘operator<<’
    main.cpp:40: error: invalid operands of types ‘const char [26]’ and ‘const char [2]’ to binary ‘operator<<’

    -------------------------
    Edit:

    I put parenthesis around them like so:

    cout << (line == "C++Draw Object File V1.0") << "\n";
    cout << (line == "C++Draw Object Fidle V1.0") << "\n";

    Compiles, but now BOTH lines output 0.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    0 means false.

    What both your original code and the latest version are saying is that your strings are different than what is in the file.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Quote Originally Posted by Daved View Post
    0 means false.

    What both your original code and the latest version are saying is that your strings are different than what is in the file.
    Right...

    I'm asking how to make it work properly hehe.

    Even when the text of the strings are exactly the same ("C++Draw Object File V1.0"), it STILL gives false.

    THIS is what I am asking on how to fix.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Have you examined the string to find out if it is really what you expect?
    Code:
    std::cout << "'" << line << "'\n";
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Quote Originally Posted by CornedBee View Post
    Have you examined the string to find out if it is really what you expect?
    Code:
    std::cout << "'" << line << "'\n";
    Genius!

    'C++Draw Object File V1.0 '

    (Has a blank at the end)

    After some Googling, I found that I should probably use string.find() instead. This will avoid this pesky problem in case the professor's input file has spaces at the end. Thanks all.

    If anyone has a better solution than string.find(), definitely post it. This thread is on my email subscription list (instant email hehe)
    Last edited by Paul22000; 05-24-2008 at 04:42 PM.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Genius!
    Experienced.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Haha! Yes I will save this in my brain alongside the dreaded "if ( a = b )" mistake

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Paul22000 View Post
    Haha! Yes I will save this in my brain alongside the dreaded "if ( a = b )" mistake
    Well that's one bug the compiler should find for you (assuming you turn your Warning Levels high enough).

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. Trying to compare to strings...
    By imortal in forum C++ Programming
    Replies: 9
    Last Post: 05-16-2003, 12:27 PM
  5. how do i compare strings
    By bart in forum C++ Programming
    Replies: 17
    Last Post: 08-30-2001, 09:17 PM