i am working on my class lab on how to search for a valid email from and input file.
below is my code, i dont really know what i am doing wrong.
can you please give me a guide
thanks
#include <iostream>
#include<string>
#include<fstream>
usingnamespace std;
bool isValidEmailCharacter (char c) // to be fix
{
bool result = false;
if (c == '_' && c== '.') result = true;
if (c == '-' && c == '+') result = true;
if (c >='a' && c <= 'z' || c >= 'A'&& c <= 'Z')result = true;
if (c >= '0' && c<= '9') result = true;
return result;
}//isValidEmailCharacter
bool hasDot()
{
bool result = false;
char hasdot;
if(hasdot =='.')result = true;
return result;
}//hasDot
//main program
int main()
{
//program introduction
introduction();
//variables
string dFilename; // default input file name
string IFile; // the user input file name using the console
string OFile; // the user output file name using the console
string userrespond; // user respond to continue
ifstream fin ; // required for file input
ifstream fout; // required for file output
string line; // each line in the input file
int i; // loop counter
int s ;
int e ;
// promt user for file input or use default input file
dFilename = "fileContainingEmails.txt";
cout<<"Enter input filename [default: fileContainingEmails.txt]: "<<endl;
getline(cin,IFile);
if(IFile.length() ==0)
{
IFile = dFilename;
}
else
{
IFile = IFile;
}
// promt the user for output file
dFilename = "copyPasteMyEmails.txt";
cout<<"Enter output filename [default: copyPasteMyEmails.txt]: "<<endl;
getline(cin,OFile);
if(OFile.length() == 0)
{
OFile = dFilename;
}
else
{
OFile =OFile;
}
// output user working files
cout<<"Your input file name is : "<<IFile<<endl;
cout<<"Your output file name is : "<<OFile<<endl;
// promt the user to continue the program
cout << "Press Enter Key to continue: ";
getline(cin,userrespond);
// open the input file
fin.open(IFile.c_str());
if(!fin.good())cout<<"Invalid file : "<<IFile<<endl;
// read the input file
while(fin.good())
{
getline(fin, line);
for(i=0; i< line.length() ; i++)
{
if (line[i] =='@')
{
for( s= i;s > 0 ; s--)
{
if(isValidEmailCharacter(s) == false);
}//for
s = s+1;
for(e = i; e < line.length(); e++)
{
if(hasDot()== true);
if(isValidEmailCharacter(e) == false);
}
}//if
if(s<i && e>i && hasDot()==true)
string anEmail = line.substr(s, e-s);
}//for close
}// while close
fin.close();
} //main
OP: could you please explain in simple words:
a. what is it exactly that your program is trying to do. What you wrote previously makes no sense unfortunately
how to search for a valid email from and input file.
what does it mean to search for a valid email?
b. what is the layout of the input "fileContainingEmails.txt" or better still post a sample input file, thank you
@ Thomas, it compile but it did not read out any email from the user input files.
- the user can input any file or use the default file for input
- I am having problem in searching for a valid email with from user input files with the following criterials
•Email addresses consist of the characters A‐Z, a‐z, 0‐9, underscore, dot, hyphen, and plus.
•Also, they must have exactly one '@' followed by, but not adjacent to, at least one '.'.
i was clear to the stage of user inputting a file, and reading the file
the place i am confused is the nexted loop to search for the valid email AFTER READING the line of the input file to '@'. when the 'i' pointer is at 'i''
bool isValidEmailCharacter (char c) // to be fix
{
bool result = false;
if (c == '_' || c== '.') result = true;
if (c == '-' || c == '+') result = true;
if (c >='a' && c <= 'z' || c >= 'A' && c <= 'Z')result = true;
if (c >= '0' && c<= '9') result = true;
return result;
}//isValidEmailCharacter
bool hasDot(char c)
{
bool result = false;
if(c=='.') result = true;
return result;
}//hasDot
//main program
int main()
{
//program introduction
introduction();
//variables
string dFilename; // default input file name
string IFile; // the user input file name using the console
string OFile; // the user output file name using the console
string userrespond; // user respond to continue
ifstream fin ; // required for file input
ifstream fout; // required for file output
string line; // each line in the input file
int i; // loop counter
int s ;
int e ;
// promt user for file input or use default input file
dFilename = "fileContainingEmails.txt";
cout<<"Enter input filename [default: fileContainingEmails.txt]: "<<endl;
getline(cin,IFile);
I would put the function to check the email in a separate function, like bool validEmail(const string& email)
and call this function for each line you read from the input.
Inside this function I would split the validation into two parts.
First part would check if the email contains any illegal chars and return false if it does.
Second part would check that the email consists of one '@' and one '.' after the '@'.
To do it you store the pos of the '@' in a variable and store the pos of '.' in a second variable - searching from the first pos. To be valid the second pos must be at least 2 bigger than the first pos.
Hope this makes any sense.