Results 1 to 3 of 3

Thread: How to validate cin input?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396

    How to validate cin input?

    Now I am making transition from C to C++.

    In the past using C, I validated by using getchar or gets, then I validated them.

    For example, the input is supposed to be integer, I will check the string for any non-numeric char, if no, I will convert it to int.

    Now I was writing a program using cout and getchar. It compiles alright and links alright but it hangs after cout and not getting any input from me. ????

    I have changed all to cin as I am the only one who use the program so I can't enter any invalid input, given the fact the program is written by me too.

    Given the scenario, the program's end user is not me, how do I verify?

    Thanks.
    I'm a VB6 beginner.

  2. #2
    Addicted Member
    Join Date
    Aug 2001
    Location
    I'm mobile
    Posts
    166
    To validate users input you can use cctype.h with its functions like isalpha(), isdigit(), etc.......

    To convert from integer to double, I think you can just write double(entered numbers).
    [p r a e t o r i a n]

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    int i;
    cin >> i;
    You don't need to validate. i will be an integer no matter what (worst case: i will be 0).
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width