请帮助我在此程序中验证电子邮件地址时遇到问题 我正在努力。我的最后两件事是为sam@this.com和 电话号码为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;
}
}
答案 0 :(得分:2)
if
中的checkemail
语句使用的是赋值运算符,而不是比较运算符。