Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

This topic is 5544 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello! Tja... I am surprised a little.
if (atoi(&StringErgebnis[i]) < atoi(&StringZahl[i]))
{
++Anzahl;
cout << StringErgebnis[i] << " < " << StringZahl[i] << endl;
}
the output is here: "2 < 2"!! Can somebody tell me, why there is this error? StringErgebnis and StringZahl are std::strings, both of them have the same size. Thanks, Christian [edited by - Austrian Coder on August 18, 2003 3:34:44 PM]

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by Austrian Coder
Hello!

Tja... I am surprised a little.

if (atoi(&StringErgebnis[i]) < atoi(&StringZahl[i]))
{
++Anzahl;
cout << StringErgebnis[i] << " < " << StringZahl[i] << endl;
}


the output is here: "2 < 2"!!
Can somebody tell me, why there is this error?
StringErgebnis and StringZahl are std::strings, both of them have the same size.

Thanks, Christian





well, you''re missing a subscript in the if line... Ah... no yer not, it just counted it as an italics marker... hehe...

it looks like it should work...

Share this post


Link to post
Share on other sites
atoi() takes a const char *. I don't think you really want that, because it looks like you just want to compare the characters at position i, correct? In that case, just do:


if (StringErgebnis[i] < StringZahl[i])
{
...
}


[edited by - fizban75 on August 18, 2003 3:30:58 PM]

Share this post


Link to post
Share on other sites
Just FYI and to expand on prior posts... atoi() expects a null-terminated string, not a single byte. The reason your code didn''t work is that atoi() was reading as many bytes as it could until it found a null, and trying to evaluate it. Depending on how your strings are set up, this can lead to the infamous "undefined behavior."

I just wanted to mention this so that you recognize this situation when it comes up again. Remember, when you use pointers to memory (* operator, & operator, ** operator, and so forth) and strings are involved, be sure your null terminators are in place. Direct memory references can be very nasty to debug.

Good luck with your code.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!