Link to home
Start Free TrialLog in
Avatar of PGRBryant
PGRBryant

asked on

String Compare in C

I'm doing a quick string compare in C and I keep getting the incorrect results.

I'm guessing that the way that a string is stored differently than it's actual displayed value, or there's otherwise some cranny behind the scenes of the coding that is causing this to occur.

Anyway, if I run code like the following, I get the display of

02
Hello

Which doesn't make any sense, any ideas?
string Letter = "02";

		cout << Letter << "\n";
		
		if (Letter.compare("01") == 1) {
			cout << "Hello" << "\n";
		}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of xchange
xchange
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PGRBryant
PGRBryant

ASKER

Ahhh,

My actual problem was a little more complicated, I had a series of If statements that followed that question which asked

Is it "01" is it "02" is it "03"...is it "15"... else... with each one redefining Letter.

And I kept getting an "else" result, but now I realized that it was because the redefinition was just causing them all to display false.. thanks.

Is 0 always true and 1 always false, can you redefine that, how does that work in C?
>Is 0 always true and 1 always false, can you redefine that, how does that work in C?
If you mean to change the function's behaviour you can do that through overloading I suppose - the details evade my memory - but do you REALLY want to do this?
(Just because you can does not mean you should - right?)
SOLUTION
Avatar of phoffric
phoffric

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'm trying to learn C but the best compiler option that I could find was C++ 2010 Express, so I figured I'd just get a little acquainted with both along the way.

And xchange and you both answered my question, so points awarded and much thanks.
Well good luck in your studies! I wish you the best. I think VS C++ 2010 Express is one of the best IDE's around for learning purposes.

It would be helpful for us and for searching the PAQ (Previously Answered Questions) by separating your C and C++ question into two separate questions, one in the C zone, and one in the C++ zone. For the C zone, be sure to post only C code so as not to confuse anyone. For the C++ zone, if you indicate that you are not looking for C-like answers, then you will get responses that hopefully will address the best C++ solutions. (I have seen pure C solutions being given in C++ zones; and I think I may have been guilty of that myself.)

By asking a C zone question and posting C++ code is very confusing to me. It also is like asking two different questions in one question. Please take a look at this:
     https://www.experts-exchange.com/help.jsp?hi=23

Again, I wish you the best in your studies.