3

When I attempt to compare two strings in C++ (one read in from a file, the other assigned a value), I get an incorrect output:

ifstream pgmFile; pgmFile.open(fileName); string temp; string comp = "P5"; for(int i = 0; i < 2; i++) { pgmFile >> noskipws >> temp; cout << temp; } if(temp == comp) {} else cout << "File does not contain 'P5'. Please provide the correct type of file." << endl; 

In theory this SHOULD return "true" that these two strings are correct. The output for temp = "P5", so I don't understand why it hits my else case every single time. Any help is much appreciated thanks!

2
  • Confirm that temp does indeed contain "P5" and not, say, " P5". See if this provides any insight: cout << '|' << temp << '|' << temp.size(); Commented Sep 19, 2013 at 19:55
  • What's in your temp? looks as you read nonskipws istream twice and your temp is the second time of what you read. Commented Sep 19, 2013 at 20:05

3 Answers 3

4

Most likely this happens because you read the value into temp twice (in your first for loop). And the second time you read an empty string (status should be EOF, for example). When you print an empty string, you don't see it. Then, you compare '' with 'P5', and those two things are not the same.

You can move printing statement after the "for" loop to see what value it holds, and that would be what is compared against 'P5'.

Hope it helps. Good Luck!

Sign up to request clarification or add additional context in comments.

1 Comment

I would vote you up if I had any rep. This is exactly what was happening. I changed my "readin" to "getline" so that I didn't overwrite my string. Thanks a lot!
1

noskipws is going to load whitespace characters ('\r', '\n', '\t', etc.) into the string. If you check the length of temp, it is likely different than the length of comp due to the whitespace at the end (if I had to guess, I'd say it is probably a line-feed).

EDIT: I glossed right over the loop - which you don't need and the second read is likely setting the pgmFile.bad flag (since the first read would have read the entire file if "P5" is all that is in it).

Comments

-3

It appears that some implementations of the std::string have an operator==() which will successfully compare, but that is not universal, as shown by std::string reference.

If that is the case in your environment, then the following applies:

It comes down to what you are testing for equal. temp and comp are different strings, so have different addresses in memory. In short, they are not equal in that sense.

What you are needing to do is to compare the characters in the string. String has a compare() function to do this, that will return -1, 0 or 1 depending on the relative strings.

8 Comments

Sorry, you're thinking in Java. C++ has a perfectly useful operator==() that compares 2 strings.
"What you are needing to do is to compare the characters in the string"... which is exactly what string::operator= does.
I think you will find that string::operator== is not universal. see cplusplus.com/reference/string/string which does not include that operator declaration.
@Ptolemy you should take a look at en.cppreference.com/w/cpp/string/basic_string
That has not always been the case. Call me an old fogie, but back when I did C++ stl code for a living, doing == on std::string was a bug.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.