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]
Hello!
Tja... I am surprised a little.
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...
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:
[edited by - fizban75 on August 18, 2003 3:30:58 PM]
if (StringErgebnis[i] < StringZahl[i]){...}
[edited by - fizban75 on August 18, 2003 3:30:58 PM]
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.
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.
Yuck, nasty mixture of C++ and C. :/
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]
[edited by - Lektrix on August 18, 2003 6:05:11 PM]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]
[edited by - Lektrix on August 18, 2003 6:05:11 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement