string temp is equal to "ZERO:\t.WORD\t1" from my debugger. (the first line of my file)
string temp = RemoveWhiteSpace(data);
int i = 0;
if ( temp.length() > 0 && isalpha(temp[0]) )
cout << "without true worked" << endl;
if ( temp.length() > 0 && isalpha(temp[0]) == true )
cout << "with true worked" << endl;
This is my code to check if first character of temp is a a-z,A-Z. The first if statement will evaluate to true and the 2nd to false. WHY?!?!?! I have tried this even without the "temp.length() > 0 &&" and it still evaluates false. It just hates the "== true". The only thing I can think of is that isalpha() returns != 0 and true == 1. Then, you could get isalpha() == 2 != 1. But, I have no idea if C++ is that ... weird.
BTW, I dont need to know that the "== true" is logically pointless. I know.
output was
without true worked
Compiled with CodeBlock using GNU GCC on Ubuntu 9.10 (if this matters any)
cout << isalpha(temp[0]) << endl;
? – Rooketrue
) if indeed c is an alphabetic letter". Which is why you shouldn't use cplusplus.com:true
in C++ doesn't mean the same thing as "true" in the description in the C standard that they copied from. opengroup.org says, "The isalpha() function shall return non-zero if c is an alphabetic character", which is rather better. – Splice<cctype>
or<ctype.h>
, and the function templatestd::isalpha
in<locale>
. The latter returnsbool
, so if you were calling that then your code would work. – Spliceisalpha(temp[0])
is wrong. You should writeisalpha((unsigned char)(temp[0]))
, or a static_cast if you insist. For reasons unclear to me,isalpha
takes anint
parameter which must be non-negative (or EOF), whereaschar
might be a signed type on your compiler (I believe it is if you're on Ubuntu on x86). Doesn't make a whole lot of sense, but there you go. I guess it was considered easier for callers to cast when reading from a string, than to check for EOF when reading from a stream. – Splicetrue
/false
is bad style because it can lead to pitfalls like what you've run into. – Anything