#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0

    Check variable types


    I am wondering, how do i prevent users from entering wrong data types.

    for example i declare x as an integer but instead of entering a integer they enter a character, and well my program bugs out.

    So then can i check to see if they enter the correct data type and if not re-prompt them to enter another value.

    Thanks in advance! :)
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    1. Read everything into a buffer using fgets()
    2. Try to convert the string to whatever data type you're interested in.
    3. Check the status result of your conversion function.

    Comments on this post

    • InfinityCounter agrees
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0
    Originally Posted by salem
    1. Read everything into a buffer using fgets()
    2. Try to convert the string to whatever data type you're interested in.
    3. Check the status result of your conversion function.
    I am not quite sure what you mean.

    1. How do i use fget(), i have never used that function before, could you explain it.

    2. that would probably crash the program due to the large size of the number after the program is done with calculations.

    3. I didn't use any conversion functions.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0
    I am not quite sure what you mean.

    1. How do i use fget(), i have never used that function before, could you explain it.

    2. that would probably crash the program due to the large size of the number after the program is done with calculations.

    3. I didn't use any conversion functions.
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Originally Posted by InfinityCounter
    1. How do i use fget(), i have never used that function before, could you explain it.
    RTFM! ("Read The Manual!")

    If your development environment is in Windows, then read the help file. If you're using gcc, then read the man page; man pages are available on-line, so you can Google on, for example, man page fgets.

    Originally Posted by InfinityCounter
    2. that would probably crash the program due to the large size of the number after the program is done with calculations.
    That was not part of the original question. Also, overflow does not normally cause the program to crash, but rather to yield bogus results.

    Originally Posted by InfinityCounter
    3. I didn't use any conversion functions.
    What? Then what did you use? That input is a string of characters, which you somehow converted to an integer value. How did you do that if you didn't use any conversion functions?

    Instead of expecting us to read your mind (HINT: We cannot read your mind!) and trying to make us waste our time by forcing us to play stupid guessing games, why don't you post your code so that we can know what you are talking about?

IMN logo majestic logo threadwatch logo seochat tools logo