简体   繁体   中英

Trying to print invalid email address from a list using c++?

I am trying to print a list of invalid emailaddress (which has a space and does not have a @ or.) from a list of email addresses. The list has a few email addresses which have spaces, and no '@' or '.' but still it does not print anything.

    //Declaring boolean variables
    bool atPresent;
    bool periodPresent;
    bool spacePresent;
    
    string emailid = someemailfrom a list;
    atPresent = false;
    periodPresent = false;
    spacePresent = false;
    
    //looking for @
    size_t foundAt = emailid.find('@');
    if (foundAt != string::npos) {
        atPresent = true;
    }
    
    //looking for '.'
    size_t foundPeriod = emailid.find('.');
    if (foundPeriod != string::npos) {
        periodPresent = true;
    }
    
    //looking for ' '
    size_t foundSpace = emailid.find(' ');
    if (foundSpace != string::npos) {
        spacePresent = true;
    }
    
    //checking to see if all conditions match
    if ( (atPresent == false) && (periodPresent == false) && (spacePresent == true)) {
        cout << emailid << endl;
    }
(atPresent == false) && (periodPresent == false) && (spacePresent == true)

Is wrong. It is only true, when all of the three criteria for an invalid adress are met. But an address is invalid as soon as at least on criteria is met. This would be

(atPresent == false) || (periodPresent == false) || (spacePresent == true)

And simplified:

!atPresent || !periodPresent || spacePresent

replace && statements by ||statements: you are only printing those which doesn't have @ AND have a space AND have a period. You should use a regex, so you can do it on one line, and know how to use them is always usefull when you try to validate user data

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM