In: Computer Science
I am trying to solve a c++ problem over c strings where I have to input an email address and check if it is valid. After completing my code I keep getting errors that my program will not run, specifically the lines with my for loops. Can you please look at my code and tell me what is wrong and how I can fix the code? I included below the assignment instructions and my current code.
Assignment Instructions
Write a method that will have a C++ string passed to it. The string will be an email address and the method will return a bool to indicate that the email address is valid.
Email Validation Rules: ( These rules are not official.)
1) No whitespace allowed
2) 1 and only 1 @ symbol
3) 1 and only 1 period allowed after the @ symbol. Periods can exist before the @ sign.
4) 3 and only 3 characters after the period is required.
Program will prompt for an email address and report if it is valid or not according to the rules posted above. The program will loop and prompt me to continue.
My Current Code:
#include<iostream>
#include <string>
using namespace std;
string emailAddress;
bool validEmail(string emailAddress)
{
int whiteSpace = 0;
int at = 0;
int period = 0;
int characterCount = 0;
for (int index = 0; index < emailAddress.length;
index++) // Error
{
if (emailAddress[index] == '
')
whiteSpace++;
else if (emailAddress[index] ==
'@')
at++;
}
if (whiteSpace > 0 || at != 1)
return false;
else
{
for (int index = at + 1; index <
emailAddress.length; index++) // Error
{
if
(emailAddress[index] == '.')
period++;
}
if (period != 1)
return
false;
else
{
for (int index =
period + 1; index < emailAddress.length; index++) //Error
{
characterCount++;
}
if
(characterCount == 3)
return true;
else
return false;
}
}
}
int main()
{
while (true);
{
cout << "Enter an email
address and I will check if it's valid" << endl;
getline(cin, emailAddress);
validEmail(emailAddress);
if (true)
cout <<
"This email address is valid";
else
cout <<
"This email address is not valid";
}
return 0;
}
#include<iostream>
#include <string>
using namespace std;
string emailAddress;
bool validEmail(string emailAddress)
{
int whiteSpace = 0;
int at = 0;
int period = 0;
int characterCount = 0;
int at_index=0;
int period_index=0;
for (int index = 0; index < emailAddress.length();
index++)
{
if (emailAddress[index] == ' ')
whiteSpace++;
else if (emailAddress[index] == '@')
{
at++;
at_index=index;
//cout<<at;
}
}
if (whiteSpace > 0 || at != 1)
{
// cout<<"hai";
return false;
}
else
{
for (int index = at_index + 1; index < emailAddress.length();
index++)
{
if (emailAddress[index] == '.')
{
period_index=index;
period++;
}
}
if (period != 1)
{
// cout<<"hmm";
return false;
}
else
{
for (int index = period_index + 1; index <
emailAddress.length(); index++)
{
characterCount++;
}
if (characterCount == 3)
{
// cout<<"hello";
return true;
}
else
{
// cout<<"hellooo";
return false;
}
}
}
}
int main()
{
bool val;
while (true)
{
cout << "\nEnter an email address and I will check if it's
valid" << endl;
getline(cin, emailAddress);
val=validEmail(emailAddress);
if (val)
cout << "\nThis email address is valid";
else
cout << "\nThis email address is not valid";
}
return 0;
}