Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

String Compare in C

Posted on 2011-03-07
7
Medium Priority
?
682 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:PGRBryant
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 2

Accepted Solution

by:
xchange earned 1336 total points
ID: 35062331
You assume that upon the strings being equal the returned value is 1 which is in fact not.
Compare returns zero upon string equality.
0
 
LVL 2

Assisted Solution

by:xchange
xchange earned 1336 total points
ID: 35062378
"02" (the object) is "GREATER" than "01" the parameter and according to the function's spec:
+1 : a positive is returned.

Let me know if you need any additional clarification.
0
 
LVL 1

Author Comment

by:PGRBryant
ID: 35062468
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?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:xchange
ID: 35062596
>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?)
0
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 664 total points
ID: 35063455
Your zone and question indicate the C language, but your code post indicates the C++ language. Why are you interested in C suggestions when writing in C++?

Since you are writing in C and using the string class (which is stored differently than your c-style string, as you already guessed), you are naturally using the string compare function.

To see how to use the compare , here is code from
     http://www.cplusplus.com/reference/string/string/compare/

Notice that there are a number of different flavors of compare giving you quite a bit of flexibility.
// comparing apples with apples
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string str1 ("green apple");
  string str2 ("red apple");

  if (str1.compare(str2) != 0)
    cout << str1 << " is not " << str2 << "\n";

  if (str1.compare(6,5,"apple") == 0)
    cout << "still, " << str1 << " is an apple\n";

  if (str2.compare(str2.size()-5,5,"apple") == 0)
    cout << "and " << str2 << " is also an apple\n";

  if (str1.compare(6,5,str2,4,5) == 0)
    cout << "therefore, both are apples\n";

  return 0;
}

Open in new window

0
 
LVL 1

Author Closing Comment

by:PGRBryant
ID: 35066473
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.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35066734
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:
     http://www.experts-exchange.com/help.jsp?hi=23

Again, I wish you the best in your studies.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Simple Linear Regression

596 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question