Jump to content
  • Advertisement
Sign in to follow this  
mattnenterprise

cctype

This topic is 3712 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!