Home Register Calendar Today's Posts FAQ Free Publications Search
 > IsNumeric in C++???

#1
06-29-2003, 10:59 AM
 Mad_Kitten Junior Contributor Join Date: Feb 2003 Location: Belgium Posts: 383
IsNumeric in C++???

 I'm very new to c++. I'm making a console application. Is there a equivalent of the IsNumeric function in VB for C++? Any help would be greatly appreciated.
#2
06-29-2003, 02:21 PM
 Zorn Centurion Join Date: May 2003 Location: South of Heaven Posts: 160

 If you are talking about input coming in as characters or a string then the ctype header has a bunch of functions for testing different kinds of character types. Two things to note. The data has to be in character format and it only checks one character. So you would have to write your own routine to check a string. C++ is a strongly typed language and if you enter a character into an int field the program will loop. Here is a little sample to check input for numerics. I'll even use the name you posted for the function. This will also allow you to enter a decimal. Code: #include #include // has all the character testers #include // used for getch(); to stop flow. int isNumeric(char*); // prototype for isNumeric // demonstrate isdigit, isalpha, //--------------------------------------------------------------------------- int main() { char *str; cout << "Input some characters or numbers" << endl; cin.getline(str,80); if(isNumeric(str)) cout << str << " is a number."; else cout << str << " is not a number."; cout << endl << "Any key to exit."; getch(); return 0; } //--------------------------------------------------------------------------- // isNumeric function. Returns 0 = if not numeric, !=0 if numeric. int isNumeric(char* str) { int len = strlen(str); // get the length of string for iterations int i = 0; // iterator int ret = 1; // for the return type int deccnt = 0; // decimal counter while(i < len && ret != 0) { if(str[i] == '.') // is there a decimal { deccnt++; // count a decimal if(deccnt > 1) // is there too many decimal points ret = 0; // too many decimals set return for not numeric } else ret = isdigit(str[i]); // is this character numeric i++; // increment to next character } return ret; // return result =0 not numeric !=0 is numeric }
#3
06-29-2003, 02:55 PM
 Mad_Kitten Junior Contributor Join Date: Feb 2003 Location: Belgium Posts: 383

 When I try your code, it gives an error on this line. #include fatal error C1083: Cannot open include file: 'ctype': No such file or directory
#4
06-29-2003, 06:44 PM
 Zorn Centurion Join Date: May 2003 Location: South of Heaven Posts: 160

 Quote:Originally Posted by GeudensWhen I try your code, it gives an error on this line. #include fatal error C1083: Cannot open include file: 'ctype': No such file or directory #include // try adding the .h some compilers don't like plain old ctype or Although it doesn't appear to be on my computer but make the 'c' a capital 'C'. ctype is part of the standard C Run Time Library I would be surprised that your compiler wouldn't have it. All standard headers should be in a directory called \include which should be in the compilers directory in most cases. If you find it in another directory than you have a couple of options. Explicitly define the path: #include "C:\headers\where\they\shouldnt\be\ctype.h" or If you find it in the next directory deeper of the include, then you can do this: #include Remember to use the directory slashes in the way your OS expects them. Here I used windows style. One other far fetched idea is that for some reason your compiler does not have the include in it's environment path. But I seriously doubt that since it appears the other headers are working. The portability for the ctype functions goes across all OS's. So even if your on another OS other than windows you should have it.

 Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 Thread Tools Display Modes Linear Mode

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home Announcements     Announcements Visual Basic .NET (2002/2003/2005/2008, including Express editions)     .NET Knowledge Base         Tutors' Corner         Code Library     ASP.Net     .NET Communications     .NET Game Programming         Managed DirectX     .NET Office Automation     .NET File I/O and Registry     .NET Database and Reporting     .NET Interface and Graphics     .NET Installation / Documentation     .NET General     Xtreme .NET Talk Legacy Visual Basic (VB 4/5/6)     Knowledge Base         Tutors' Corner         Code Library     Communications     Game Programming         DirectX     VBA / Office Integration         Excel         Word, PowerPoint, Outlook, and Other Office Products     API     File I/O and Registry     Database and Reporting     Interface and Graphics     Installation / Documentation     General Other Languages     Web Programming     Miscellaneous Languages General Discussion     Member Offers and Deals     Tech Discussions     Random Thoughts     Forum Questions / Concerns / Comments

 Similar Threads Thread Thread Starter Forum Replies Last Post sdjukic General 3 06-28-2003 05:06 AM bobba buoy Web Programming 3 06-04-2003 05:02 AM stauf General 9 06-14-2002 11:14 AM DanielB General 4 03-14-2002 06:57 AM Defiance General 5 07-01-2001 11:31 AM