composition and validation

Asked

Viewed 83 times

0

Guys I have a struct called Contact with variables Std::string

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

so I did something like this below but this giving segmentation failure or has how to do better so that I can solve this by receiving within the class builder an obeto type contact?

#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";

}

1 answer

1


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

Start watching the warnings!

  • it did not give any specific Warning itself, only showed segmentation failure even as before running again I changed the type of the email* print function that would give error because I am;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.