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] = '  ';

Recommended Answers

All 3 Replies

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];

use:

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,

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.