-4

Basically, I wish to limit the user to input in the correct format of email address with the code below.

 cout << "Donor's Email: ";
 cin >> email;
     while (email != "@" && email != "."){
     cout << "Please enter correct email format." << endl;
     cout << "Donor's Email: ";
     cin >> email;
     }

Somehow, the results are, even I input the correct format of email address but it keeps on looping for me to input again. Somebody please help me. Thanks.

KoXing
  • 3
  • 2
  • 1
    `==` and `!=` compare the whole string. It looks like you actually want to check if it contains a character instead. – interjay Oct 18 '17 at 10:45
  • Have a look here for using regular expressions [https://stackoverflow.com/questions/36903985/email-validation-in-c] – Jan Fischer Oct 18 '17 at 10:47

2 Answers2

3

Don't you want the negation of your condition: i.e.

while (!(email != "@" && email != "."))

which, by application of De Morgan's law simplifies to

while (email == "@" || email == ".")

But this seems to me to be an insufficient check for validity (surely "@@" is invalid too, for example). Consider using the regular expression library std::regex &c. from C++11, having Googled "regular expression for a valid email address".

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 1
    This won't help. It looks like this user is confusing equality checking with substring search. i.e. they want to check that the string contains both `@` and `.`. – interjay Oct 18 '17 at 10:44
  • Meh. The regular expression solution is still appropriate. And a character by character check will not weed out multiple `'@'``, unless you take extra steps. – Bathsheba Oct 18 '17 at 10:47
  • 1
    But you didn't even provide that solution in your answer... and most of your answer is still incorrect. – interjay Oct 18 '17 at 10:48
  • @interjay: Perhaps you could show us all how it should be answered then? – Bathsheba Oct 18 '17 at 10:49
  • I don't like answering such low-effort questions. But "google how to use regular expressions" is at best a comment. And I don't understand why you're leaving the first part of the answer up, given that it's clearly wrong. – interjay Oct 18 '17 at 10:49
2

Didn't try but if you're using C++11 you could use std::regex.

From gonjay's answer your code could be something like:

 #include <regex>
 using namespace std;

 const regex pattern("(\\w+)(\\.|_)?(\\w*)@(\\w+)(\\.(\\w+))+");
 cout << "Donor's Email: ";
 cin >> email;
      while (!regex_match(email, pattern)){
      cout << "Please enter correct email format." << endl;
      cout << "Donor's Email: ";
      cin >> email;
      }
Grifo
  • 71
  • 1
  • 10
  • Could you fashion this into compilable code? Also, I shied away from supplying an actual regular expression since one that works for all possibilities doesn't exist! – Bathsheba Oct 18 '17 at 11:01
  • 1
    I guess this is a quite complex matter. Here: https://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address?page=1&tab=votes#tab-top I know there are no regular expression that works for every email, but since that, to do this properly, the complexity would increase significantly, I decided to give a quick fix answer. Again, didn't try this code. – Grifo Oct 18 '17 at 11:06
  • Yes, it's not simple; I was thinking about pasting up the RFC 5322 regular expression in my answer, but thought better of it ;-) That all said, I think what you have is a good enough drop-in replacement for an upvote. – Bathsheba Oct 18 '17 at 11:08
  • If you did, stack OVERFLOW would live up to its name – Grifo Oct 18 '17 at 11:12