Glitchen
Glitchen

Reputation: 47

Comparing strings in this instances work differently, any reason?

Can someone help me with this, please? The block of code in the green box works fine, but the one in red works but not really. It's checking if the emails have both "@gmail.com" and "@yahoo.com". I want it to check both emails so that if they both contain only one "@gmail.com" or "@yahoo.com", it will exit the loop. Thank you!

#include <iostream>
using namespace std;

int main()
{
    string emailOne_, emailTwo_;
    bool valid = true;

do
{
    cout << "Email: ";
    cin >> emailOne_;
    cout << "Re-enter email: ";
    cin >> emailTwo_;

    if (emailOne_.compare(emailTwo_) != 0)
    {
        valid = false;
        cerr << "\t[*] ERROR: EMAILS DO NOT MATCH\n\n";
    }
    else
    {
        valid = true;
    }

    string myArray[] = { "@gmail.com", "@yahoo.com" };
    int arrSize = sizeof(myArray) / sizeof(myArray[0]);
    for (int i = 0; i < arrSize; i++)
    {
        auto found = emailOne_.find(myArray[i]);
        if (found == string::npos)
        {
            valid = false;
            cerr << "\t[*] ERROR: EMAILS MUST HAVE @gmail.com or @yahoo.com\n\n";
            break;
        }
        else
        {
            valid = true;
        }
    }
} while (valid == false);

return 0;
}

Upvotes: 0

Views: 87

Answers (1)

Ted Lyngmo
Ted Lyngmo

Reputation: 117583

You have some issues in the code

  • auto found = emailOne_.find(myArray[i]); will find the @gmail.com even if the entered email address is [email protected], which is probably not what you want.
  • If the first entry in myArray doesn't match, you break out and don't test the next.
  • If the first entry is a match, you don't break out, so you go on and try the next string in myArray which will of course not match - and then you break out.

It's therefore currently impossible to get a valid match. By breaking out only when a valid match is found, you should be able to get the correct result.

Example with some suggested simplifications:

#include <iostream>
#include <string>

int main() {
    const std::string myArray[] = {"@gmail.com", "@yahoo.com"};

    std::string emailOne_;

    for(;;) {
        std::string emailTwo_;
        std::cout << "Email: ";
        std::cin >> emailOne_;
        std::cout << "Re-enter email: ";
        std::cin >> emailTwo_;

        // simplify comparisons:
        if(emailOne_ != emailTwo_) {
            std::cerr << "\t[*] ERROR: EMAILS DO NOT MATCH\n\n";
            continue; // back to entering email
        }

        bool valid = false;

        // Use range-based for loops to simplify looping over arrays:
        for(const std::string& end : myArray) {

            // check if the end of emailOne_ is equal to `end` by comparing
            // a substring of emailOne_ with the same length as end, with end:

            if(emailOne_.size() > end.size() &&   // must be long enough
               emailOne_.substr(emailOne_.size() - end.size()) == end)
            {
                valid = true;
                break;
            }
        }

        // check the valid state after the loop:
        if(!valid) {
            std::cerr << "\t[*] ERROR: EMAILS MUST HAVE @gmail.com or @yahoo.com\n";
        } else {
            break; // a valid email address was found.
        }
    }

    std::cout << emailOne_ << " is a valid address\n";
}

Upvotes: 1

Related Questions