'Warning when comparing integers of different signs
I've two very similar functions in 2 programs as follows.
/*
* Function: isalphaString
* Usage: isalphaString(str);
* ----------------------------
* Judge if all characters in the string is alpha.
*/
bool isalphaString(string str) {
for (int i = 0; i < str.size(); i++) {
if (! isalpha(str[i]))
return false;
}
return true;
}
the second function:
bool containsNonAlpha(string boardText) {
for (int i = 0; i < boardText.size(); i++) {
if (!isalpha(boardText[i])) {
return true;
}
}
return false;
}
but get two different compiling results. The first one I have no compiling warnings, but the second one I received a warning: "comparison of integers of different signs" . Not sure why this happens.
Solution 1:[1]
You can't compare an int with std::string::size_type, you need to change this line:
for (int i = 0; i < boardText.size(); i++)
to:
for (string::size_type i = 0; i < boardText.size(); i++)
altought you can use a range-loop in C++11 that is much simpler:
for(char &c : boardText) {
check_if_alpha(c);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jean Pierre Dudey |
