C++ Class Validation

I am trying to figure out how to verify that the assignment of the character array and the ssn actually have something in it, but I am having difficulty in setting that up.  The ssn is not supposed to be =< 0 and the name is not supposed to be blank.  

How do I revert to the default constructor if the values entered ( programmed into ) since there is no input do not meet the required specs?  

Thanks.



#include <iostream>
#include <iomanip>
 
 
using namespace std;
 
class Student
{
      // private member variables
      private:
              long socialSecurity;
              char name[80];
     
     
      // public variables / methods       
      public:
             
             // default constructor
             Student(char *n="unassigned",long sn=999999999);
            
             // Name get / set
             void setName(char *n);
             char *Student::getName();
            
             // Social Security set / get
             void setSSN(long sn);
             long getSSN();
           
            
};
 
// default constructor
Student::Student(char *n, long sn)
{
    strcpy(name, n);
    socialSecurity = sn;
}
 
 
// implementation section
void Student::setName(char *n)
{
 
         strcpy(name, n);
}
 
// Student setSSN()
void Student::setSSN(long sn)
{
    
     socialSecurity = sn;
    
}
 
char *Student::getName()
{
     return name;
}
 
 
// Student getSSN
long Student::getSSN()
{
     return socialSecurity;
}
 
int main()
{
 
    // instantiate class objects
    Student student1;
    Student student2;
   
    
    
    // set class object 2
    student2.setName("John Doe");   
    student2.setSSN(11122555);
   
    // class output
    // output stream for student 1 using default constructor
       cout << "The name for student1 is " << student1.getName()
            << " and ssn is " << student1.getSSN() << endl;
   
    // output stream for student 2
       cout << "The name for student2 is " << student2.getName() 
            << " and ssn is " << student2.getSSN() << endl;
         
        
   
    system("pause");
    return 0;
   
}

Open in new window

LVL 1
lwoodtriAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mgonulluCommented:
What you have to do is the followiing:
Keep the constructor you already implemented and then add an empty constructor where you use the set members
It should be something like this: in the definition part

 Student(char *n="unassigned",long sn=999999999);
Student();



In the implementation keep the constructor you already did and add the other one

Student::Student()
{
   setName("Unassigned");
    setSSN(999999);;
}

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lwoodtriAuthor Commented:
Cool, I got that part . . however . . what if the assignment happens to be 0 or -1 for the value of SSN?  Would I use an

if ( ssn =< 0 || ssn > 999999999)?

would that work for validation?  
0
mgonulluCommented:
For sure that would work!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.