Thread: MSVC Signed/Unsigned Warning

  1. #1
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719

    MSVC Signed/Unsigned Warning

    I got this warning for a while now and it's starting to ANGER me! Here is the code that produces the warning:
    Code:
    for(int i=0;i < text.size();i++)  //warning here
    	{
    		cout << text[i];
    		Sleep(interval);
    	}
    Here is the warning:
    Code:
    1>c:\documents and settings\tony\my documents\visual studio 2005\projects\darkastarrpg\darkastarrpg\utility.cpp(8) : warning C4018: '<' : signed/unsigned mismatch
    Is using size() valid for finding the length of a string?
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Try changing the type of i from int to unsigned int.

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Method size returns a size_t, which is unsigned.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Is using size() valid for finding the length of a string?
    Yes, but it doesn't return an int. It returns a string::size_type. So you could use:
    Code:
    for (string::size_type i=0; i<text.size(); i++)
    Or:
    Code:
    for (std::string::size_type i=0; i<text.size(); i++)

  5. #5
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    That worked. I never would've figured this out. Is their a reference for these 'keywords', where it tells the return type, etc.?
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Is their a reference for these 'keywords', where it tells the return type, etc.?
    Here are two references:
    http://www.cppreference.com
    http://www.sgi.com/tech/stl/

    Note the second one (sgi) use the name basic_string for string.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    MSDN is a third.

    And they're not keywords. They're just functions.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The compiler gives you a warning when comparing signed and unsigned values because the signed value will be converted to an unsigned one. This could cause problems if the signed value is negative (among other situations); if(unsign < -1) turns into if(unsign < -1u). -1u is the largest unsigned value, so the if would always be false.

    In general, you should use the same variable type on both side of the equation.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    It would make sense it would be unsigned, because you can't have a negative size. I understand now, thanks!
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM