Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C++
negative number evaluating greater than string.size()
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Jason, post: 1482238"] Hi, below is example code which demonstrates a problem I have encountered. When passing a number to a function I compare it with a string's size and then take certain actions, unfortunately during the testing of it I discovered that negative numbers were being treated as if they were > 0. I compiled the following on mingw compiler/dev c++/windows xp. If I replace string.size() for a ordinary number it behaves as expected? I notice string.size() returns size type and not an int but how do I deal with that? Thanks in advance for any help #include <iostream> #include <stdlib.h> #include <string> using namespace std; void something(int); int main() { something(-1); system("PAUSE"); return 0; } void something(int number) { string str = "sdfjksdflksdjfkjsklfjsklfj"; if(number < str.size()) { cout << " hello" << endl; } else { cout << " eek" << endl; } } [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C++
negative number evaluating greater than string.size()
Top