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.

Recommended Answers

All 2 Replies

You could write a function that goes through the strings that are entered and checks that the letters all make sense. For this, you can use the functions in <cctype> , you can read more about these here. You can, for example, use isalpha to check if the character is an alphabetic letter (not some punctuation or a number or something) and then you can use toupper() to convert the first letter to an upper case letter, etc. I would imagine that this function might be something like this:

bool isGoodName(std::string s){
    for(std::string::iterator it = s.begin(); it != s.end(); ++s)
        if(isalpha(*it) != true)    return false;

    return true;
}

Maybe a second function could capitalise the first letter:

void capitalise(std::string &s){
    if(s.size() > 1)    s[0] = toupper(s[0]);
}

Might not be exactly what you want, but I hope that helps a little :)

ravenous's solution is the most portable, but if the cctype functions are not allowed in your assignment, use characters in your inequalities:

char c = 'd';
if(c >= 'a' && c<='z') //use || to include the capitals c >='A' && c<='Z' etc
  etc.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.