在 c++ 中验证电子邮件时遇到问题;

发布于 2024-12-10 14:15:05 字数 3177 浏览 0 评论 0原文

请帮助我在验证此程序中的电子邮件地址时遇到问题 正在努力。我的最后两件事是验证电子邮件的 [email protected] 和 将电话号码转换为 xxx-xxx-xxxx 格式。这是代码:

#include<iostream>
using namespace std;

class contact{
private:
    string lname;
    string fname;
    string address;
    string email;
    string phonenumber;//bool checkphonenumber(string phonenumber)

public:
    void output();
    void input();
    bool checkemail(string email);

    //constructor name has to be the same as class
    contact(string contact_lname,//parameters
            string contact_fname,
            string contact_address,
            string contact_phonenumber,
            string contact_email    ){

        lname = contact_lname;
        fname = contact_fname;
        address = contact_address;
        phonenumber =  contact_phonenumber;//bool checkphonenumber(string phonenumber)
        email = contact_email;
    }
    contact(){//set all variables to null
        lname = "";
        fname = "";
        address = "";
        phonenumber = "";
        email =  "";
    }
    //set
    void setlname(string contact_lname){lname = contact_lname;}
    void setfname(string contact_fname){fname = contact_fname;}
    void setAddress(string contact_address){address = contact_address;}
    //get
    string getlname(){return lname;}
    string getfname(){return fname;}
    string getaddress(){return address;}

};//end class
//to prevent overload run function outside
void contact::output()
{
    cout << "Contact name is: " << fname <<" "<< lname <<endl;
    cout << "Address is: " << address << endl;//address is not been filtered
    cout << "Email Address is: " << email << endl;


}
bool contact::checkemail(string email) {
    for(int a = 0; a < email.size(); a++) {
        if(email.at(a) = '@') return true;
    }
    return false;
}
void contact::input(){
    cout<<"enter last name: ";
    cin>>lname;
    cout<<"enter first name: ";
    cin>>fname;
    cout<<"Enter address: ";
    cin>>address;
    cout<<"enter email ";
    cin>>email;
    while (!checkemail(email)) {
        cout << "that is an invalid email address, re-entry email address." ;
        cin>>email;
    }

    cout<<"enter phone number ";
    cin>>phonenumber;

}
int main(){

    contact c;
    c.input();
    c.output();
    return 0;
}

这是我遇到问题的代码部分。

这是需要验证电子邮件的部分。

bool contact::checkemail(string email) {
    for(int a = 0; a < email.size(); a++) {
        if(email.at(a) = '@') return true;
    }
    return false;
}
void contact::input(){
    cout<<"enter last name: ";
    cin>>lname;
    cout<<"enter first name: ";
    cin>>fname;
    cout<<"Enter address: ";
    cin>>address;
    cout<<"enter email ";
    cin>>email;
    while (!checkemail(email)) {
        cout << "that is an invalid email address, re-entry email address." ;
        cin>>email;
    }
}

Please help I'm having problem validating email address in this program that I
am working on. My last two things are to valid the email for a [email protected] and
the phone number into a xxx-xxx-xxxx format. Here is the code:

#include<iostream>
using namespace std;

class contact{
private:
    string lname;
    string fname;
    string address;
    string email;
    string phonenumber;//bool checkphonenumber(string phonenumber)

public:
    void output();
    void input();
    bool checkemail(string email);

    //constructor name has to be the same as class
    contact(string contact_lname,//parameters
            string contact_fname,
            string contact_address,
            string contact_phonenumber,
            string contact_email    ){

        lname = contact_lname;
        fname = contact_fname;
        address = contact_address;
        phonenumber =  contact_phonenumber;//bool checkphonenumber(string phonenumber)
        email = contact_email;
    }
    contact(){//set all variables to null
        lname = "";
        fname = "";
        address = "";
        phonenumber = "";
        email =  "";
    }
    //set
    void setlname(string contact_lname){lname = contact_lname;}
    void setfname(string contact_fname){fname = contact_fname;}
    void setAddress(string contact_address){address = contact_address;}
    //get
    string getlname(){return lname;}
    string getfname(){return fname;}
    string getaddress(){return address;}

};//end class
//to prevent overload run function outside
void contact::output()
{
    cout << "Contact name is: " << fname <<" "<< lname <<endl;
    cout << "Address is: " << address << endl;//address is not been filtered
    cout << "Email Address is: " << email << endl;


}
bool contact::checkemail(string email) {
    for(int a = 0; a < email.size(); a++) {
        if(email.at(a) = '@') return true;
    }
    return false;
}
void contact::input(){
    cout<<"enter last name: ";
    cin>>lname;
    cout<<"enter first name: ";
    cin>>fname;
    cout<<"Enter address: ";
    cin>>address;
    cout<<"enter email ";
    cin>>email;
    while (!checkemail(email)) {
        cout << "that is an invalid email address, re-entry email address." ;
        cin>>email;
    }

    cout<<"enter phone number ";
    cin>>phonenumber;

}
int main(){

    contact c;
    c.input();
    c.output();
    return 0;
}

This is the part of the code that I'm having problem with.

This is the part that needs to validate the email.

bool contact::checkemail(string email) {
    for(int a = 0; a < email.size(); a++) {
        if(email.at(a) = '@') return true;
    }
    return false;
}
void contact::input(){
    cout<<"enter last name: ";
    cin>>lname;
    cout<<"enter first name: ";
    cin>>fname;
    cout<<"Enter address: ";
    cin>>address;
    cout<<"enter email ";
    cin>>email;
    while (!checkemail(email)) {
        cout << "that is an invalid email address, re-entry email address." ;
        cin>>email;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诗笺 2024-12-17 14:15:05

checkemail 中的 if 语句使用的是赋值运算符,而不是比较运算符。

The if statement in checkemail is using an assignment operator, not a comparison operator.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文