Thread: does not equal

  1. #16
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    Lightbulb here..

    I worked my butt out (to improve it) and I came up with this :

    PHP Code:
    int checknumber(char array[20])
    {
        
    int i;
        
    int minuscount 0;
        
    int pointcount 0;

        
    int bad 0;

        for (
    i=0; array[i]; i++)
        {
            if ( array[
    i] == 45)
            {
                
    minuscount++;
            }

            if ( array[
    i] == 46)
            {
                
    pointcount++;
            }

            if( array[
    0] == NULL || array[i] < 45 || array[i] == 47 || array[i] > 57 )
            {
                
    bad 1;
            }
        
            if(
    i>&& array[i] == 45)
            {
                
    bad 1;
            }
        }
        
        if (
    minuscount || pointcount 1)
        {
            
    bad 1;
        }

        return 
    bad;    

    The function returns 0 for no errors and 1 if there are errors

    LIMITATIONS :
    1) . is valid
    2) -. is valid
    3) x. is valid (where x is a number)
    4) array can't have more than 20 elements. increase this if you want.
    Also, if you're using cin, ' ' (space) will give strange problems.

    I hope this will be of some use to you.

  2. #17
    Sir Mister Insane Sako Klinerr1's Avatar
    Join Date
    May 2002
    Posts
    608
    thanks it helped alot. i modified it a little thouhg but it helped a lot!
    Email: [email protected] || AIM: MisterSako || MSN: [email protected]

    -the shroom has spoken

  3. #18
    Unregistered
    Guest
    There is a group of istream methods; good(), fail(), clear(); that can do input error checking for you, too. You should look up the syntax, but I think it something like this:

    Code:
    cout << "input a floating point number;
    float num;
    cin >> num;
    if(cin.fail())//or if(!cin.good())
    {
      cout << "input error, not a valid floating point number" << endl;
      cin.clear();
    }
    else
      cout << num << endl;

  4. #19
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Don't count on good() catching bad input beyond the first digit.

    Where it works nicely is in a simple console-based program with a menu, for example, where a single digit is expected as input. However, as long as the first character read into the stream is a digit, you can put in anything after that and the compiler will accept the input as valid.

    As you might discern, it makes no distinction between ints and floats. I know there are a lot of folks that wish that determining the validity of a float were that simple.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Equal compare
    By George2 in forum C Programming
    Replies: 1
    Last Post: 11-10-2007, 05:26 AM
  2. compare if 2 string types are equal
    By sujeet1 in forum C++ Programming
    Replies: 4
    Last Post: 06-06-2007, 06:37 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. set string array equal variable
    By WaterNut in forum C++ Programming
    Replies: 3
    Last Post: 06-29-2004, 05:02 PM
  5. how can 0.00665 not equal 0.00665??!!
    By Susan in forum C++ Programming
    Replies: 10
    Last Post: 02-10-2002, 02:33 AM