Sign in to follow this  
mattnenterprise

cctype

Recommended Posts

I an having a problem testing if a character is lower case. char ch; cin.get(ch); while(ch != '@') { if(islower(ch)) cout << ch; else cout << "enter a character << endl; cin.get(ch); } when I try to use this it always uses else instead of either if or else

Share this post


Link to post
Share on other sites
I havn't used the isLower function myself but you may want to roll your own method. If you take a look at an ascii table, you will see lower case letters begin at 97 decimal and end at 122 decimal. With that info you can do a simple check if the character is within this region, if so then the character is lowercase.


#include <iostream>

bool isLowerCase(char c)
{
if(((int)c >= 97) && ((int)c <= 122))
return true;
else
return false;
}

int main()
{
char userInput;

std::cout << "Lower case letter test, use '@' to exit" << std::endl;

while(userInput != '@')
{
std::cout << "Please enter a lower case letter: ";
userInput = std::cin.get();

if(isLowerCase(userInput))
{
std::cout << userInput << std::endl;
}
else
{
std::cout << "That isn't a lower case letter!" << std::endl;
}
}
}


Share this post


Link to post
Share on other sites
Quote:
Original post by cNoob
I havn't used the isLower function myself but you may want to roll your own method. If you take a look at an ascii table, you will see lower case letters begin at 97 decimal and end at 122 decimal. With that info you can do a simple check if the character is within this region, if so then the character is lowercase.

*** Source Snippet Removed ***

You actually don't need to typecast it to int. A char is simply a 1 byte integer, so you can compare it to actual numbers.

Share this post


Link to post
Share on other sites
Quote:
Original post by MikeTacular
You actually don't need to typecast it to int. A char is simply a 1 byte integer, so you can compare it to actual numbers.


It's a silly habbit of mine I picked up from an old book :P

Share this post


Link to post
Share on other sites
islower() returns a non-zero value if the character is lowercase. It returns zero if the character is uppercase. So, you could test to see if the character is lowercase like this.


char ch;

while(ch != '@')
{
cin.get(ch);

if(islower(ch) != 0) // if not a zero, it's lowercase
cout << ch << endl;

else
cout << "enter a character" << endl;

cin.get(ch);
}

Share this post


Link to post
Share on other sites

inline const bool isLowerCase(char c)
{
return c >= 'a' && c <= 'z';
}



Declared inline because it appears to be written in a cpp file (function declared in header files are implicitely declared inline).
Returns const bool so that dumb errors like if (isLowerCase('A') = true) { } do not compile.

Share this post


Link to post
Share on other sites
Quote:
Original post by Nyarlath
inline bool isLowerCase(char c)
{
return c >= 'a' && c <= 'z';
}


Just as a warning - some people using computers do not use English/US alphabet, and the above won't work.

In general, determining the case of letters is an absurdly complex problem.

This is why in general it's marginally better to use built-in functions, or, if testing the case is really important, using OS-provided, or third-party I18N libraries.

And while it may not seem like beginner topic, there's plenty of people who googled for the solution, then scratched their heads over why entering their name of home town seems to break the application.

Share this post


Link to post
Share on other sites
Unless it has to do with non-ascii characters, it might be better if the OP posted the exact code. I can't believe that std::islower wouldn't work for any character.

Quote:

Returns const bool so that dumb errors like if (isLowerCase('A') = true) { } do not compile.


That dumb error would cause a compile error without the const too, because the return value is not modifiable anyhow. [smile]

Share this post


Link to post
Share on other sites
Quote:
Original post by Nyarlath
*** Source Snippet Removed ***

Declared inline because it appears to be written in a cpp file (function declared in header files are implicitely declared inline).


Functions defined in class definitions (which themselves typically occur in header files) are implicitly inline. All other functions are not, regardless of whether or not they exist in a header file. The compiler does not understand what a header file is and cannot afford it any special privileges -- by the time the compiler sees the translation unit, the preprocessor has mucked it all together into one big in-memory file.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this