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!