Archived

This topic is now archived and is closed to further replies.

cMADsc

isalpha()

Recommended Posts

cMADsc    122
Greetings all, I was looking at my orginal guess-the-number game in C/C++ using MSVC 6.0, just visualizing how far my coding has inhanced. Although, I went back making some correction, added some code to check to see if a char was entered instead of a int, I used "isalpha()": ========================================= int guess; cin >> guess if(isalpha(guess)) cout << "Error you entered a char.\n"; . . ====================================================== Unexpectly the program seemed to run in an infinite loop. When I went back and did a simple programming using "isalpha()" like something I saw on the net it worked. Then retring the programming, sometimes it work, but mostly it did not. Any enlightenment to what is going on is certainly appreciated. ----------------------------- "There are ones that say they can and there are those who actually do." "...u can not learn programming in a class, you have to learn it on your own."

Share this post


Link to post
Share on other sites
Kylotan    9871
You can''t really use the data types like that. If you use cin with an int, then there is no way of detecting whether the user enters a char or not. You only call isalpha on letters in a string, generally. Using cin will convert their characters to the appropriate number automatically, providing that they only enter numbers. If they enter letters, you have to check for errors in other ways, as isalpha is not for use in this way.

I know that''s not a very good explanation... can someone else explain this?

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

Share this post


Link to post
Share on other sites
EvilCrap    134
do u mean a number instead of a character? char and int are data types... to test a number vs a character, isalpha or isdigit are fine. or, for alphanumeric, you can use !isspace.

if your getting an infinate loop it may be because somewhere you cin, say, 5 bytes, and intend to cin only 4... now ur next cin may not work, therefore messing a test up, and hince an infinate loop.

when you get data in loops, use getline, not >>, its *much* safer. also check for other memory errors, like setstr on uninitialized char*s (a null might not exist until beyond the array...)

Share this post


Link to post
Share on other sites