Validating entries of type double

This is a discussion on Validating entries of type double within the C++ Programming forums, part of the General Programming Boards category; Okay, I'm writing a program which will allow the user to input double values into an array until the array ...

  1. #1
    Unregistered
    Guest

    Post Validating entries of type double

    Okay, I'm writing a program which will allow the user to input double values into an array until the array is full or the user enters non-numeric input. I've tried using cin.fail() with a while loop, which works fine for invalid input like:

    abc
    a12
    *!2

    However, if the user enters something like:

    123t
    1trig
    45UU87

    The cin will read it in until a character is reached and then it has not failed. It only fails after the numbers. How do I get it to exit the loop when this happens, without storing the 123 of 123T into the array, etc.

    Help is much appreciated.

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    maybe you could read it as a string and reject the whole thing if any non numerical parts are in it. that seems wasteful, though. my 2 cents.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-20-2008, 08:59 AM
  2. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 04:44 PM
  3. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  4. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 09:38 PM
  5. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 09:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21