Thread: Problem checking for numeric value in a structure

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

    Unhappy Problem checking for numeric value in a structure

    Hello,

    I've been trying to figure out why I can not check for a valid numeric field in a strucutre. Here are parts of my program:

    The structure...

    // 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;

    "StudentID" is listed as an integer field. Right now, I am attempting to check for a valid numeric field.

    the function prototpye, calling function and function itself:

    void InputStudentID( StudentRecord& ); // Prototype for the InputFirstName function

    InputStudentID(Student);

    void InputStudentID(StudentRecord& aStudent) // Function heading
    {

    do
    {
    cout << endl << "Enter Student's ID #: ";
    cin >> (aStudent.StudentID);

    if (!cin)
    {
    cout << "The Student's ID # must be numeric. Please enter the amount again." << endl;
    cin.clear();
    cin.ignore(10, '\n');
    }
    }
    while (!cin);

    return;
    }



    From what our instructor told us, when dealing with integers, you can use the notication "if (!cin)..." to test whether the user has entered valid numeric fields. I have attempted to set up a simple do..while loop to repeat the steps of entering a student id until the user enters the information correctly. The loop should simply repeat while telling the user to enter a valid numeric field but for some reason it goes out of the loop and quits the program. I don't see any logic error on this unless there is something different about setting up the integer field under a structure that I'm not aware of.

    Your suggestions will be most appreciated.

    Thank you!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > if (!cin)
    If this is true,

    and you do this
    > cin.clear();

    Then this won't be true
    > while (!cin);

    Consider this perhaps
    Code:
    bool again;
    do { 
      cout << endl << "Enter Student's ID #: "; 
      cin >> (aStudent.StudentID); 
      again = false;  // assume the best...
    
      if (!cin)  { 
        cout << "The Student's ID # must be numeric. Please enter the amount again." << endl; 
        cin.clear(); 
        cin.ignore(10, '\n'); 
        again = true;
      } 
    } while ( again );

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

    Talking Thanks!

    Hello Salem,

    Thanks for the suggestion. There's still a problem though. I put in a boolean as you suggested to change my logic slightly and it APPEARS to be working fine if you put in a non-numeric character as the FIRST character you type. For example:

    A134
    y
    b
    ^135325

    The program appears to repeat fine as long as you type a non-numeric character as the FIRST character. If I try values like these:

    1A23
    1234*
    2A2B

    For some reason it is leaving the loop and ending my program even with that boolean in there. I honestly don't know what it's not liking about the second set of errors I type. If you can see anything I'm doing wrong with my logic, please let me know.

    Your help has been greatly appreciated!

    void InputStudentID(StudentRecord& aStudent) // Function heading
    {

    bool do_it_again;

    do
    {
    cout << endl << "Enter Student's ID #: ";
    cin >> (aStudent.StudentID);
    do_it_again = false;

    if (!cin)
    {
    cout << "The Student's ID # must be numeric. Please enter the amount again." << endl;
    cin.clear();
    cin.ignore(10, '\n');
    do_it_again = true;
    }
    }
    while (do_it_again);

    return;
    }

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    I think personally your best bet is to use cin.getline() rather than cin>> and use string processing techniques ( isdigit() comes in handy) to check that the input is valid and the when you are sure that it is you can convert the string to an integer by using atoi().
    The problem you are having is that if your first char is an int (well you know what i mean) is that cin stops reading at the first non numeric char, reads the int before and leaves junk in the stream for the next input operation to screw up on.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

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

    Thumbs up Thanks Stoned..

    I will look into using the cin.get. I had a previous program whereby I followed the same logic and it worked perfectly. I wonder tho if reading a reference point of a structure data type is causing a problem. My other program was a simple integer variable passed by value.

    Thanks man!

    Ron

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with structure !!!!
    By m_kaleia in forum C Programming
    Replies: 2
    Last Post: 04-19-2009, 06:41 AM
  2. Structure problem
    By lolguy in forum C Programming
    Replies: 4
    Last Post: 12-18-2008, 10:32 PM
  3. Problem about Creating structure
    By albert3721 in forum C Programming
    Replies: 3
    Last Post: 06-05-2007, 07:33 PM
  4. Replies: 9
    Last Post: 05-21-2007, 12:10 AM
  5. Floating point checking problem
    By ikkiutsu in forum C Programming
    Replies: 8
    Last Post: 12-03-2003, 06:35 AM