Here is my code, I would like to check my input for errors in the first and lastname parts. it means that these two parts should contin just upper and lower case letter and hyphens(cin must be used as well).
#include <iostream>
#include <string>
#include<cstdlib>
using namespace std;
struct Student {
string firstName;
string lastName;
unsigned number;
unsigned units;
unsigned gradePoints;
};
void input(Student &x);
void output(Student &x);
int main()
{
Student NewStudent;
input(NewStudent);
output(NewStudent);
return 0;
}
void input(Student &x)
{
cout<<"Enter your First Name:\n"; /*this part*/
cin>>x.firstName;
cout<<"Enter your last Name: \n";
cin>>x.lastName; /*this part*/
cout<<"Enter you Student Number: \n";
cin>>x.number;
if(x.number<100000000 || x.number>999999999)
{
cout<<"invalid student number\n";
exit(EXIT_FAILURE);
}
cout<<"How many units you have taken: \n";
cin>>x.units;
if(x.units>1000000 || x.units<0)
{
cout<<"Invalid number of units: \n";
exit(EXIT_FAILURE);
}
cout<<"Enter your number of gradePoints: ";
cin>>x.gradePoints;
if(x.gradePoints> 4 * x.units)
{
cout<<"Invalid total number of gradepoints:\n ";
exit(EXIT_FAILURE);
}
}
void output(Student &x)
{
cout<<"Your name is: "<<x.firstName<<" "<<x.lastName<<endl;
cout<<"Your student number is "<<"#"<<x.number<<endl;
cout<<"You have taken "<<x.units<<" units"<<endl;
cout<<"Your total grade points are "<<x.gradePoints<<endl;
cout<<"Your GPA is"<<(x.gradePoints)/x.units<<endl;
}
It is working but I do not how to check for above conditions just for firstname and lastname.