composition and validation

0

Personal I have a struct called Contact with variables std :: string

and a ready class of mail validation using regex. I am creating a program and do not need to create another validation function for this contact class I made the include of my email class so I have in my struct Contact a function of type contact called getContact.

So I did something of the type below but this is giving segmentation failure or how can I do it better so that I can solve this by getting a contact object inside the class constructor?

#include <regex>
#include <iostream>

struct Contact
{
    std::string eMail;
}; 

class email
{
  std::string _mail;
  Contact e;

 public:

   email(Contact& em): _mail(em.eMail){}

 bool isMail()
 {
  std::smatch email_smatch;

  const std::regex pattern("([a-zA-Z0-9._]+@(?:(?:hotmail|terra|yahoo|bol)[.](?:com[.]br)?)?(?:(?:gmail)[.](?:com)?)?)?");

  return std::regex_match(_mail, email_smatch, pattern);
 }

 email* print()
 { 
   e.eMail = _mail;
   std::cout<<"\n\tEmail: "<<(email(e).isMail()?" is Valid\n":" is Invalid\n"); 
 }
};


Contact getContact()
{
  Contact c;
     do{
        std::cout << "\n\tEnter email: ";
         getline(std::cin, c.eMail);

         email(c).print();

       }while(email(c).print() == 0);
}

int main(void)
{
  getContact();
  std::cout<<"\n";

}
    
asked by anonymous 24.12.2017 / 16:24

1 answer

1

The problem is you declared the function "getContact" as type "Contact" but at the end of the function you did not return the value.

Start watching the warnings!

    
24.12.2017 / 17:55