So I have a program that validates email. It test for things like an @ sign.
However, when the function that does this called in main, it does not work.
I've tried different things but have no idea on how to get this to work. Any help is appreciated.
int main()
{
char code [4] = {'z','z','z','z'};
char input[25];
int result;
char p1 [13];
char p2 [13];
cout << setw(52) << "Welcome to Open Email Exchange. \n";
cout << setw (53) << "---------------------------------- \n";
cout << " - This program accepts ALL email addresses. You will need to provide \n";
cout << setw(37) << "an email address and password. \n \n";
cout << "Please enter your email address: ";
cin.getline ( input , 50 );
//check the email's size
result = strlen(input);
email (input , result , code );
return 0;
}
bool email (char string[] , int size , char code [] )
{
bool valid = false;
//Contain an @ symbol that appears before any periods
if ( string [size-4] == '.')
{
valid = true;
for (int i = size-5 ; i < size ; i --)
{
if ( !string[i] == '@')
{
code [0] = 'F';
cout << "You do not have an @ symbol (must be before any periods) \n";
}
else
code [0] = 'T';
}
}
//Contain period followed by three letters
if (string[size-4] == '.')
{
valid = true;
for (int i = size -3 ; i < size ; i ++)
{
if ( !isalpha(string[i]))
{
valid = false;
code [1] = 'F';
cout << "You must have a period followed by three letters. (i.e. webster.edu.) \n";
}
else
{
valid = true;
code [1] = 'P';
}
}
}
//Contain at least three characters between @ and period
return valid;
}
You email function sets the first char in the code array, it prints an error message, but does not return false (in the first if condition). The second if condition code overwrites the work done by the first one.
So you either need to use else if, or make use of a switch statement - which would be easier and scalable to deal with multiple situations mentioned below.
What were you going to do with the value in the code array?
You only set the first char anyway with one of three values, so it could be two bool variables in main. Then have 2 functions because there a 4 situations - bad @ symbol, or bad placement of the dot, or a combination of both. So you just need to return true or false from the functions instead then analyse what is bad or good. At the moment you do not assign the value returned from the function to anything.
The code does not cope with email addresses that end with 2 chars like someone@hotmail.com.au or someone@ComapanyName.co
Also why do you not make use of C++ strings? You can refer to individual chars in a similar way to arrays. You can take advantage of built-in functions.
There are changes afoot in the internet, where the final tag can be personalised. Addresses might be www.documentary.bbctv instead of www.bbc.co.uk/documentary. I am not sure whether there will be email addresses associated with such websites.