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!