I am trying to create an email validation program without using the regex library. In one of my functions, I want to return a Boolean to check if there is an @ sign in the email address and also if it is in a valid position (the @ sign cannot be one of the first three characters of the string). However I am having problems with it because every time I run the program by entering in an email address with the @ sign in an invalid position, it keeps telling me that the email is valid. Please help!
valid = checkEmail(email); //function call
if(valid == true)
cout << "Your email is valid!" << endl;
else
cout << "Your email is invalid!" << endl;
bool checkEmail(string email)
{
int counter;
int length;
bool firstThree; //checks to make sure @ is not in the first three chars
counter = 0;
length = email.length();
firstThree = false;
for(int i = 0; i < length; i++)
{
if(email[i] == '@')
counter++;
}
for(int y = 0; y < length; y++)
{
if(email[0] == '@' || email[1] == '@' || email[2] == '@')
firstThree = true;
else
firstThree = false;
}
cout << "\n" << counter << endl; //check to see if counter works
if(counter != 1 && firstThree == true)
return false;
else
return true;
}