Go Back  Xtreme Visual Basic Talk > Other Languages > Miscellaneous Languages > IsNumeric in C++???


Reply
 
Thread Tools Display Modes
  #1  
Old 06-29-2003, 10:59 AM
Mad_Kitten's Avatar
Mad_Kitten Mad_Kitten is offline
Junior Contributor
 
Join Date: Feb 2003
Location: Belgium
Posts: 383
Default 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.
Reply With Quote
  #2  
Old 06-29-2003, 02:21 PM
Zorn's Avatar
Zorn Zorn is offline
Centurion
 
Join Date: May 2003
Location: South of Heaven
Posts: 160
Default

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 <iostream.h>
#include <ctype>                  // has all the character testers
#include <conio>                  // 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
}
Reply With Quote
  #3  
Old 06-29-2003, 02:55 PM
Mad_Kitten's Avatar
Mad_Kitten Mad_Kitten is offline
Junior Contributor
 
Join Date: Feb 2003
Location: Belgium
Posts: 383
Default

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

Quote:
Originally Posted by Geudens
When I try your code, it gives an error on this line.
#include <ctype>
fatal error C1083: Cannot open include file: 'ctype': No such file or directory



#include <ctype.h> // 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 <deeper\ctype.h>
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.
Reply With Quote
Reply


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

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 Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
InputBox and IsNumeric sdjukic General 3 06-28-2003 05:06 AM
IsNumeric in JavaScript bobba buoy Web Programming 3 06-04-2003 05:02 AM
Isnumeric Problem stauf General 9 06-14-2002 11:14 AM
IsNumeric problem/bug? DanielB General 4 03-14-2002 06:57 AM
isNumeric Syntax Defiance General 5 07-01-2001 11:31 AM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->