Why do I get this error/incorrect syntax? Do you know how I can fix this?

comparison between signed and unsigned integer expressions

for ( int j = 0; j < item.length(); j++)  // error ocurs here
       if ( item[j] == '_' )
	item[j] = '  ';
8 Years
Discussion Span
Last Post by u8sand

That's not an error, it's a warning. And C++ std::string::length() returns an unsigned int. You're using an int for your counter in your loop so the comparison j < item.length() is comparing a signed int (default int) to an unsigned int


Always use the size_t type for indexing a container, or for container size.
That means
-> instead of for ( int j = 0; j < item.length(); j++) use for (size_t j = 0; j < item.length(); j++) -> instead of

int size= 5;
int* x= new int[size];


size_t size=5;
int* x= new int[size];

size_t is the alias name for a unsigned integer type depending on your platform.
size_t may be unsigned int or unsigned long etc.
Using size_t indicates that your code will be more portable,

Votes + Comments
The actual thread solver! Including some valuable information in his post as well :)

This may be just a problem with posting on the forum, but if you copied and pasted it another error is that in your code in the single character quotes (' ') you put two spaces. two spaces is two characters not one character.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.