Home > Back-end >  write a code for checking valid email address,
write a code for checking valid email address,

Time:03-01

i was writing a code on code blocks for checking a valid email address by checking following conditions:

1.must have at least one uppercase. 2.should have characters above 8 and less than 50 3.should have @ sign

i have used 3 while loops for checking individual condition , but after entering the email address the program gets stopped . here is my code ,does anyone know what is the problem?

enter code here

#include<iostream>
using namespace std;
#include<stdio.h>
#include<conio.h>

void check_mail()
    {
    int i = 0;
    char email[25];
    int measure = 0;
    cout<<" \n \n \n enter an email address ::";
    gets(email);

    while(email[i] != '\0')//for checking uppercsae //

    {
        if( (int)email[i] >= 65  && (int)email[i] <= 90)
        {
        measure = 1;

        }
        if( measure != 1)
        {
        cout<<"\n there is no uppercase letter in the email address ";
        break;
        }
    }

    while(email[i] != '\0') //checking @ sign//
    {
        if((int)email[i] == 64)
        {
        cout<<" \n found the @ character at :: "<<i<<endl;
        }
    }

    int counter = 0;
    while(email[i] != '\0')
    {
        counter = counter  1 ;
    }
        if(counter >=8 && counter <=50)
        {
        cout<< "\n valid number of characters are present in the mail :: ";
        }
        else if(counter <8)
        {
        cout<<" \n  number of characters are less than 8 ";

        }
        else if(counter >=51 )
        {
        cout<<"\n the elements are greater than 50 ";
        }
        else
        {
            cout<<"\n enter a valid email address::";
        }
    }
int main()
{
    cout<<" \n \n enter a email address ";
    check_mail();
    return 0;
}

CodePudding user response:

This code below is a working and a way better implementation of your code:

#include <iostream>
#include <string>

bool check_mail(const std::string email)
{
    if (email.size() < 8 || email.size() > 50) return false;

    int upper_letters = 0;
    for (int i = 0; i < email.size(); i  )
    {
        if (std::isupper(email[i])) upper_letters  ;
        if (email[i] == '@')
        {
            if (i < 8) return false;
            else if (upper_letters == 0) return false;

            return true;
        }
    }
    return false;
}

int main()
{
    std::cout << " \n \n Enter an email address "; 

    std::string email; std::cin >> email;
    std::cout << check_mail(email) << std::endl;

    return 0;
}

If you need to know what exactly caused the email to get rejected, you can do the following:

#include <iostream>
#include <string>

enum email_states { correct, under_char, over_char, no_upper, no_at_the_rate };

email_states check_mail(const std::string email)
{
    if (email.size() < 8) return email_states::under_char;
    else if (email.size() > 5) return email_states::over_char;

    int upper_letters = 0;
    for (int i = 0; i < email.size(); i  )
    {
        if (std::isupper(email[i])) upper_letters  ;
        if (email[i] == '@')
        {
            if (i < 8) return email_states::under_char;
            else if (upper_letters == 0) return email_states::no_upper;

            return email_states::correct;
        }
    }
    return email_states::no_at_the_rate;
}

int main()
{
    std::cout << " \n \n Enter an email address "; 

    std::string email; std::cin >> email;
    std::cout << check_mail(email) << std::endl;

    return 0;
}

For the 2'nd code, if the output is:

0 - correct
1 - under_char
2 - over_char
3 - no_upper
4 - no_at_the_rate

Also, using namespace std is considered as a bad practice. For more info on this look up to why is "using namespace std" considered as a bad practice.

CodePudding user response:

You could consider using std::string and utilizing the standard library:

#include <iostream>
#include <string>

constexpr int kMinEmailCharacters = 8;
constexpr int kMaxEmailCharacters = 50;
constexpr char kAtSign = '@';

bool IsValidEmail(const std::string &email) {
  auto email_length = email.length();
  auto contains_uppercase = std::count_if(email.begin(), email.end(), isupper);
  auto contains_at_sign = email.find(kAtSign) != std::string::npos;
  return email_length > kMinEmailCharacters &&
         email_length < kMaxEmailCharacters && contains_uppercase &&
         contains_at_sign;
}

int main() {
  std::cout << "Enter email: ";
  std::string user_email;
  std::cin >> user_email;
  auto valid_email = IsValidEmail(user_email);
  std::cout << "Valid email: " << (valid_email ? "true" : "false") << '\n';
  return 0;
}

Example Usage 1:

Enter email: [email protected]
Valid email: false

Example Usage 2:

Enter email: [email protected]
Valid email: true

CodePudding user response:

There are some logic errors in the code:

  • in the first loop you print "there is no uppercase letter in the email address" when the first character is not an uppercase letter. You need to check all letters before you know if there was an uppercase
  • after the first loop, i is already at the end of the string, the next loop will not have a single iteration.

Further

  • use std::string
  • conio.h is windows only. You don't need it when you can use std::cin.
  • while(email[i] != '\0') is error-prone (you need to corrceclty manage the index, which your code fails to do). Use a range based loop instead
  • try to avoid magic numbers.
  • don't use C-style casts
  • in particular there is no need to cast the characters to int. You can compare directly to 'A' and 'Z'.
  • after you checked if the size of the input is <8 or >=8 && <=50 or >51 there is no other case, the else is superfluous.

Your code with this fixes:

#include<iostream>
using namespace std;

void check_mail()
    {
    int i = 0;
    std::string email;
    int measure = 0;
    cout<<" \n \n \n enter an email address ::";
    //gets(email);
    std::cin >> email;
    for (char c : email) {
        if( c >= 'A'  && c <= 'Z') { measure = 1; }
    }
    if( measure != 1) {
        cout<<"\n there is no uppercase letter in the email address ";
        //break;
    }

    for (char c : email) {
        if(c == '@') {
        cout<<" \n found the @ character at :: "<<i<<endl;
        }
    }

    int counter = email.size();
    if(counter >=8 && counter <=50) {
        cout<< "\n valid number of characters are present in the mail :: ";
    }
    else if(counter <8) {
        cout<<" \n  number of characters are less than 8 ";
    }
    else if(counter >=51 ) {
        cout<<"\n the elements are greater than 50 ";
    }
}
int main()
{
    cout<<" \n \n enter a email address ";
    check_mail();
    return 0;
}

Produces expected output for input "[email protected]":

 enter a email address  
 
 
 enter an email address :: 
 found the @ character at :: 0

 valid number of characters are present in the mail :: 

I suppose for the last output you also wanted to add email.size() to the output.

CodePudding user response:

You consider using Regex for check email:

// C   program for the above approach

#include <iostream>
#include <regex>
using namespace std;

// Function to check the email id
// is valid or not
bool isValid(const string& email)
{

    // Regular expression definition
    const regex pattern(
        "(\\w )(\\.|_)?(\\w*)@(\\w )(\\.(\\w )) ");

    // Match the string pattern
    // with regular expression
    return regex_match(email, pattern);
}

// Driver Code
int main()
{
    // Given string email
    string email = "[email protected]";

    // Function Call
    bool ans = isValid(email);

    // Print the result
    if (ans) {
        cout << email << " : "
            << "valid" << endl;
    }
    else {
        cout << email << " : "
            << "invalid" << endl;
    }
}
  •  Tags:  
  • c
  • Related