Feb 21, 2011 at 4:06am UTC
Hi
here is my program, my question is about firtname and lastname part, the question wants to check for input error , namely if user enters anything other than upper and lower case letters and hyphens for the firstname and lastname , program complains and dies immidiately. how can I do that in my program.(cin mustbe used as well.)
Thanks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
#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);
void takeClass(Student &x, unsigned N_units, char grade);
double gpa(Student &x);
int main()
{
/*unsigned N_units;
char grade;*/
Student NewStudent;
input(NewStudent);
/*output(NewStudent);
takeClass(NewStudent,3,'A');
output(NewStudent);
cout<<" GPA is equal to"<<gpa(NewStudent)<<endl;*/
return 0;
}
void input(Student &x)
{
char Answer='y' ,grade;
cout<<"Enter your First Name:\n" ; /*This part is my problem , how to check
cin>>x.firstName; for input errors.
cout<<"Enter your last Name:\n"; */
cin>>x.lastName;
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:\n" ;
cin>>x.gradePoints;
if (x.gradePoints> 4 * x.units)
{
cout<<"Invalid total number of gradepoints:\n " ;
exit(EXIT_FAILURE);
}
while (Answer=='y' )
{
cout<<"How many units you have taken:\t" ;
cin>>x.units;
if (x.units>1000000 || x.units<0)
{
cout<<"Invalid number of units:\n" ;
exit(EXIT_FAILURE);
}
cin>>grade;
if ((grade!='A' && grade!='B' && grade!='C' && grade!='D' && grade!='E' ))
{
cout<<"Invalid grade letter\n" ;
exit(EXIT_FAILURE);
}
cout<<"Would you like to continue and Enter your number of units and your grade letter?(y/n)\n" ;
cin>>Answer;
}
}
void output(Student &x)
{
cout<<"Your name is: " <<x.firstName<<" " <<x.lastName<<" " ;
cout<<"Your student number is " <<"#" <<x.number<<" " ;
cout<<"You have taken " <<x.units<<" units" <<" " ;
cout<<"Your total grade points are " <<x.gradePoints<<" " ;
cout<<"Your GPA is" <<(x.gradePoints)/x.units<<endl<<" " ;
}
void takeClass(Student &x, unsigned N_units, char grade)
{
int k;
if (N_units>24 || (grade!='A' && grade!='B' && grade!='C' && grade!='D' && grade!='E' ))
{
cout<<"Your data cannot be updated" <<endl;
exit(EXIT_FAILURE);
}
if (grade=='A' )
k=4;
else if (grade=='B' )
k=3;
else if (grade=='C' )
k=2;
else if (grade=='D' )
k=1;
else
k=0;
x.units=x.units+N_units;
x.gradePoints=x.gradePoints+k*N_units;
}
double gpa(Student &x)
{
double GPA=(x.gradePoints)/x.units;
if (x.units==0)
return 0;
else
return GPA;
}
Last edited on Feb 21, 2011 at 4:10am UTC