Thread: does not equal

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

    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)

            if ( array[
    i] == 46)

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


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

    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
    thanks it helped alot. i modified it a little thouhg but it helped a lot!
    Email: || AIM: MisterSako || MSN:

    -the shroom has spoken

  3. #18
    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:

    cout << "input a floating point number;
    float num;
    cin >> num;
    if( if(!cin.good())
      cout << "input error, not a valid floating point number" << endl;
      cout << num << endl;

  4. #19
    Registered User
    Join Date
    Apr 2002
    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.

    "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
    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