error '<' : signed/unsigned mismatch

I almost had it, my first code without errors...:-(

Can anyone help me to understand why I am getting this warning when I compile?

line 13: warning C4018: '<' : signed/unsigned mismatch

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>
using namespace std;

int main ()
{
string word;
bool palindrome;
 
cout << "Enter a word: ";
cin >> word;

for (int x=0; x < word.length()-1; x++)
{
if (word[x] != ' ')
{
if (tolower(word[x]) != tolower(word[word.length()-(x+1)]))
{
palindrome = false;
 
break;
}
else { palindrome = true; }
}
}
if (palindrome) cout << "The word is a palindrome"<<endl;
 
else cout << "The word is not a palindrome"<<endl;

return 0;
}
Last edited on
Just ignore it.
closed account (o1vk4iN6)
What... no.

The bits for a signed and unsigned int have different meaning, you could ignore it but how hard is it to make it the same time so you know for sure there won't be an error.

1
2
3
4
5
6
7
8

unsigned i = 0;

while( -1 < i-- )
{
// infinite loop
}


That's compiler determined though. Not sure how it determines asm instruction it chooses to do the comparison ( signed or unsigned ) or if it is just undefined.

Anyway C++11 introduces some ways of doing this:

1
2
3
4
5
6
7
8
9
10
11
12
13

// normal way, all standard library have type defines, as it can vary depending on the system

for( std::string::size_type x = 0; /* ... */ )
{
}

// C++11

auto x = words.length();

decltype( words.length() ) x;
Last edited on
whoa...I've never seen that before. Where does it go? Above the variable declaration?

1
2
3
4
5
6
7
8
9
int main ()
{
auto x = words.length();

decltype( words.length() ) x;

string word;

bool palindrome;


@noo1,

The tolower function returns a char, you haven't assigned this to anything, so line 17 makes no sense at all.

Edit: It is me that is not making sense!!!

The length function returns an unsigned value of type size_type, hence the error and xerzi really good solution.

Hope all goes well.
Last edited on
Thank you very much xerzi and TheIdeasMan. Your help is very much appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string>
using namespace std;

int main ()
{
string word;
bool palindrome;
char response;

do
{
cout << "Enter a word: ";
cin >> word;

for( std::string::size_type x = 0; x < word.length()-1; x++)

{
if (word[x] != ' ')
{

if (tolower(word[x]) != tolower(word[word.length()-(x+1)]))
{
palindrome = false;
 
break;
}
else { palindrome = true; }
}
}
if (palindrome) cout << "The word is a palindrome"<<endl;
 
else cout << "The word is not a palindrome"<<endl;

cout << "Would you like to enter another word? [y/n]";
cin >> response;
response = tolower(response);
}
while (response=='y');

return 0;
}
Quick question...would it have solved the problem if I'd declared
char word
or
unsigned char word
instead of my
string word
?
but then I wouldn't be able to use length right?

OK, thank you TheIdeasMan. I appreciate you clarifying that.
closed account (o1vk4iN6)
Those were just some different ways of declaring the type as in some cases you can have something like this:

1
2
3
4
5
6
7
8
9
10
std::vector< nameofsomelongclass > vect;

for( std::vector< nameofsomelongclass >::iterator it = vect.begin(); ... )
{
}

for( auto it = vect.begin(); ... )
{
}


Can probably see which is easier.

Also just to put some thought into your algorithm, if the words are palindromes then they are symmetric yah ? So would you need to test the entire word or just half of it ? Not to confuse you though, you are currently testing the whole word twice essentially.
Last edited on
xerzi, I actually thought about testing just half the word, that would make more sense.

thank you very much for explaining the type situation. I'm sure I'll be running into that over and over as I continue to learn C++
Topic archived. No new replies allowed.