checking for valid numeric field..

This is a discussion on checking for valid numeric field.. within the C++ Programming forums, part of the General Programming Boards category; I am attempting to check for a valid numeric field using the following notation: // Type declarations enum GenderType {MALE, ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    14

    Question checking for valid numeric field..

    I am attempting to check for a valid numeric field using the following notation:

    // Type declarations

    enum GenderType {MALE, FEMALE};
    enum RegistrationType {T, M, U};

    struct StudentRecord
    {
    char FirstName[31];
    char MiddleInitial;
    char LastName[31];
    int StudentID;
    GenderType Gender;
    RegistrationType Registration;
    float GPA;
    };

    StudentRecord Student;

    void InputStudentID( StudentRecord& ); // Prototype for the InputFirstNamefunction

    InputStudentID(Student);

    void InputStudentID(StudentRecord& aStudent) // Function heading
    {
    cout << endl << "Enter Student's ID #: ";
    cin >> (aStudent.StudentID);

    while (aStudent.StudentID < 1000 || aStudent.StudentID > 9999999)
    {
    cout << endl << "The Student's ID # must be numeric and be between 4 - 7 digits long."
    << endl << "Please enter the amount again." << endl;
    cin.clear();
    cin.ignore(10, '\n');
    cout << endl << "Enter Student's ID #: ";
    cin >> (aStudent.StudentID);
    }

    return;
    }

    The problem I'm having is when I type in the following:

    1111a
    11111a
    111111a
    1111111a

    All of the value should be errors. I originally used a "while (!cin).." notation to check but even this didn't work. Is there another way to check an integer to see if it's numeric? I'm stumped.

    Thanks!

  2. #2
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    gotta do it here...
    have to write here.....

    private messages only take 1000 characters....



    Hi again........
    of course there's another way to solve this...remember C++ is a very very powerful language....

    just an update:

    the reason why a program accepts the input 1111a is because the comparisons that you are doing pertain to "NUMBERS" not characters... so if you first enter a number followed by a character then the compiler will read only the numbers and if it encounters a characters it will ignore the rest....however if you will START with a character your loop most likely will become infinite and go on forever....

    the way to fix it is to send it to another function (right before it enters the loop) to have it examine the statement piece by piece whether it is a chracter or a number....

    --- first of all you would have to include another header file called
    .......... ctype.h ............

    - the function that would check for a character is ........ isalpha(char SomeLetterGoesHere)............. --- this function would return TRUE if your character is a letter...

    - the function that would check for a number is ....... isdigit(char SomeCharacterGoesHere) ............. --- this function would return TRUE if your character is a "DECIMAL" digit ....

    hope this helps a little bit....

    .... the only problem is that if you would have to "CAST" the integer so it would become a "STRING" or an array of character so that this function can examine your input "character-by-character" and then cast it back to a INTEGER ....

    well, as you can see, there's a lot more work involved in this...
    but don't get discouraged....

    Regards,
    matheo917

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    14

    Smile Thanks!

    Thanks Matt..

    Sorry for the private. I am so frustrated that this simple (!cin) notation is not working. Our instructor specifically taught us about this in previous classes so I simply looked over some of my previous projects and copied/pasted the notation in the new one thinking it would work. But it didn't

    I originally thought the way you described about checking for numerics character by character. I set it up as char StudentID[8] and looked up the (isdigit) notation and started playing around with that. But I was told I was making too much work for myself going about it this way.

    I am wondering if working with a structure variable passed as a reference is somehow causing a problem I can't see. My previous programs involved passing variables as values.

  4. #4
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    NO, passing by reference in this case is not causing any problems.....

    HINT: teachers aren't always right, nor am I, nor any book on C++, not even Stroustrup (totally) himself........

    Keep on writing programs, this is the best way to learn and solidify your knowledge......

    Good Luck,,,,

    Gotta go.......

    Regards,
    matheo917

    ps. you can catch my by e-mail more frequently........

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Please critique and suggest improvements for parser
    By Queue in forum C Programming
    Replies: 14
    Last Post: 09-28-2006, 08:28 PM
  3. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM
  4. i am not able to figure ot the starting point of this
    By youngashish in forum C++ Programming
    Replies: 7
    Last Post: 10-07-2004, 02:41 AM
  5. Problem checking for numeric value in a structure
    By ronkane in forum C++ Programming
    Replies: 4
    Last Post: 01-20-2002, 01:53 PM

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