2011-10-18 58 views
-1

請幫助我在驗證此程序中的電子郵件地址時出現了問題,我正在處理此問題 。我最後兩件事是將電子郵件地址[email protected]和 電話號碼轉換爲xxx-xxx-xxxx格式。這裏是代碼:在C++驗證電子郵件時遇到問題

#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

那到底是什麼問題呢?沒有解釋的代碼轉儲不會幫助任何人。 – chrisaycock

+0

請改善你的valide-email測試,因爲它太「輕」了。 –

+0

你的編譯器警告中有哪一個是你難以理解的?或者您是否可以驗證「today @ noon」作爲電子郵件地址? – Johnsyweb

回答

2

if聲明checkemail使用賦值運算符,而不是一個比較操作。