Thread: Checking if strings are equal

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    186

    Checking if strings are equal

    In java I know that you can never say "==" to check if two strings are equal but in C++ you can. Here's what I am having trouble with:

    Code:
                    in.open(argv[i]);
    		if(in.is_open())
    		{	
    			while(!in.eof())
     			{
     				getline(in,line);
     				cout << line << endl;
    				if(line=="\n")
    					cout << "found new line" << endl;
     			}
    		}
    		else cout << "CAN'T OPEN" << endl;
     		in.close();
    I put new lines in the file by hitting enter, and it prints the new lines but never enters that if statement. Am I doing this wrong?

    Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That's because getline() removes the newline entered. I suppose you could check for an empty string instead.

    By the way, use the return value of getline() itself to control the loop. The use of eof() is incorrect since the end of file condition is only reached after a failed read.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    Using the empty string worked. What's wrong with using eof? I'm not sure how I would use getline to control the loop. The code seems to work fine as is but since I'm new to C++ I'm always willing to learn.

    Thanks!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jcafaro10
    What's wrong with using eof?
    Since the EOF condition is only set after a failed read, after reading the last line, EOF will not be set, so you will loop one more time than you should.

    Quote Originally Posted by jcafaro10
    I'm not sure how I would use getline to control the loop.
    You can do something like this:
    Code:
    while (getline(in, line))
    {
        cout << line << endl;
        if (line.empty())
            cout << "found blank line" << endl;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    That looks prettier. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Strings under the STL?
    By laserlight in forum C++ Programming
    Replies: 9
    Last Post: 07-19-2007, 02:55 AM
  2. Problem with Strings, Please help!
    By varus in forum C++ Programming
    Replies: 8
    Last Post: 11-27-2006, 11:47 PM
  3. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  4. Help w/ comparings two strings case sensitive
    By ikkin in forum C Programming
    Replies: 7
    Last Post: 11-13-2003, 08:26 AM
  5. Table mapping Strings to Strings
    By johnmcg in forum C Programming
    Replies: 4
    Last Post: 09-05-2003, 11:04 AM