在 c++ 中验证电子邮件时遇到问题;
请帮助我在验证此程序中的电子邮件地址时遇到问题 正在努力。我的最后两件事是验证电子邮件的 [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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
checkemail
中的if
语句使用的是赋值运算符,而不是比较运算符。The
if
statement incheckemail
is using an assignment operator, not a comparison operator.