Problem with email validation program

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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.

HTH
Last edited on
With a char array its easier to count the number of characters and figure out their exact place in the array.

Hmm, I'll revise and post again.
The code array is simply a way to keep track of what each individual task is doing, and if returns as valid or invalid.
Topic archived. No new replies allowed.