Cin Test

This is a discussion on Cin Test within the C++ Programming forums, part of the General Programming Boards category; could anyone tell me a way to test if cin is a type double or int? thx...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    13

    Cin Test

    could anyone tell me a way to test if cin is a type double or int?
    thx

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    cin requires user input. I'd store user input in a string and parse the string to determine if input were valid int, valid double, or invalid as either int or double. I'd call atoi() to convert to int or atof() to convert to double.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    13
    ^
    in simple C++ language u mean? (ur talking to to first year engineer programmer)

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    It means the only type that will always have valid input from the keyboard is a string. So accept the input into a string. Then evaluate the string to see if any of the char are invalid for the type you want. For example, if there is a decimal, it can't be an int. If there is any letter other than e it can't be numeric input. If there is any non-alphabetical, non-digit char other than +, -, (, or ), then it can't be numerical. Alternatively, if all char are digits, then it can only be an int or invalid (if there are 18 digits, it won't fit in an int so its invalid). If all the char are digits except one decimal point, then it is a double. Allowing input in scientific notation is a bit trickier to validate, but not impossible. Once you have determined what type it is--int, double, invalid, then you can use the standard atoi() to convert the string to type int or atof() to convert the string to type double. Since all ints are also doubles, you will need to decide whether doubles that could be ints are ints or doubles.

    I suppose you could take advantage of the last relationship and attempt to put input into type double. If it's a valid type double then you can see if the decimal portion (mantissa?) is zero. If so, then you could arbitrarily say the number is an int, not a double.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    Quote Originally Posted by elad
    It means the only type that will always have valid input from the keyboard is a string. So accept the input into a string. Then evaluate the string to see if any of the char are invalid for the type you want. For example, if there is a decimal, it can't be an int. If there is any letter other than e it can't be numeric input. If there is any non-alphabetical, non-digit char other than +, -, (, or ), then it can't be numerical. Alternatively, if all char are digits, then it can only be an int or invalid (if there are 18 digits, it won't fit in an int so its invalid). If all the char are digits except one decimal point, then it is a double. Allowing input in scientific notation is a bit trickier to validate, but not impossible. Once you have determined what type it is--int, double, invalid, then you can use the standard atoi() to convert the string to type int or atof() to convert the string to type double. Since all ints are also doubles, you will need to decide whether doubles that could be ints are ints or doubles.

    I suppose you could take advantage of the last relationship and attempt to put input into type double. If it's a valid type double then you can see if the decimal portion (mantissa?) is zero. If so, then you could arbitrarily say the number is an int, not a double.
    what about the "0x" prefix for hexadecimal?
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    To me int and double are base 10 only. If you think that 0x is valid, I won't argue one way or the other, then you can set up the validation code to allow for that input syntax as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Integer Emulation
    By Elysia in forum C++ Programming
    Replies: 31
    Last Post: 03-18-2008, 02:03 PM
  2. undefined reference
    By 3saul in forum Linux Programming
    Replies: 12
    Last Post: 08-23-2006, 06:28 PM
  3. getline(function)??
    By dac in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2006, 12:42 PM
  4. C++ Operator Overloading help
    By Bartosz in forum C++ Programming
    Replies: 2
    Last Post: 08-17-2005, 01:55 PM
  5. MSVC Template Constructor/Assignment Errors
    By LuckY in forum Windows Programming
    Replies: 3
    Last Post: 07-22-2005, 03:57 PM

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