Reading a valid email from an input file

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
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
#include<string>
#include<fstream>
using namespace 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 
Last edited on
What are the problems? Does it compile, does it crash......
What is the input, what is the output?
One problem I can see is in
1
2
3
4
5
6
7
8
bool hasDot()
 {
   bool result = false;
   char hasdot;
   if(hasdot =='.')result = true;
   return result;
   
 }//hasDot 


hasdot has a random value so the function returns randomly true or false. What's the point of such a function without input?

Another proble is if (c == '_' && c== '.') This can never be true. Did you mean or || ?
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);

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)break;

}//for

s = s+1;

for(e = i; e < line.length(); e++)
{
if(hasDot(e)== true) break;
if(isValidEmailCharacter(e) == false)break;
;

}
}//if

if(s<i && e>i && hasDot(e)==true)
string anEmail = line.substr(s, e-s);

}//for close

}// while close


fin.close();
} //main



sample input file

Valborg Birdsong — valb_bi@webmine.com — ZA$a_AqEVE@ — f89e1b10b6967b6ad68a07712015912e
Ailen Tobler — ailen_tobler@webmine.com — ~e}y^YMYGUqY — 07b443cac004c71493fdbe207396a5e7
Dana Pickle — dana_pic@webmine.com — _y#a}Urav — 3bf5804772a365c92bc43692deb7d0c2
Selena Coots — seco@mail.com — gaZA_eZU4 — c824b73a275d69190a3de2465f13aebe
Lindley Valentine — lindley-val@carmag.com — %YguXYX — ac1f24da52fbdda056b74de209a8ec96
Noble Peng — noble-peng@yoohoo.com — rUMenY^Y — 22238e9dc662a9902dd02ebba8174890
Fae Zaragoza — fae-zaragoza@geocities.com — 4APu!AmavA= — ee39f4908e6515605a475320432944c1
Adney Perry — adney_per@carmag.com — PuzE*yNa — 6bbbd9c21adedc3cf08c4631d143d7b3
Timor Cline — ti_cl@google.com — MU&a5Y^aBE — 347dfbe9d2d2266f90f148b9ebf1e76f
Priam Ferguson — pr_ferguson@myspace.com — Gu=EryJyXY — 66f9f9a88a831dcc0fbf03e2c36c11b2
Nicholas Kelliher — nichola_kelli@linux.com — `ypavunu+u2 — 857a5e1fec37543eaf21195e4243ac7e
Ricarda Heisey — rica.heise@webmine.com — XyNaGy=uva — 25f5f8a934fe32065bbc016e0d48b5f3
Maye Tooley — may.toole@yahoo.ca — gU_EVYPu — 1e3cae7f79f346e69fbb48e876aefad4
Kaikara Hruska — kaik_hrusk@webmail.com — mU+e}A(U4u — f9bb3c86bdfbb6fb8ed4c364d0b39169
Candan Westberg — candanwe@myspace.com — !eZA(E4u^E — 72eb72216f0d95d9431b34fd3aa09343
Mosi Maddy — mos_mad@yoohoo.com — .ejYNEgaBeM — d1c5f1ffda8ec9cc7ed8ca4c9e37c7b5
Erith Lloyd — erith_llo@bing.com — SUvU^UHa#Az — d05903544fb2a0f949b69c28c3d846ff
Derwin Erskine — derwiner@freespace.com — 8YjAGY,yN — 82f8f426eb16a1dfc431e2571ccef718
Crosby Skillman — cro.skillm@linux.com — ~a3y~A&UmAnA — 511f8778a3de295d72644aa1d3b595cc
Maye Fetter — maye_fette@msn.com — VaQY/UheQ — 3d4c5e4b9075e5fe0889daaf619f19d8
Raphaella Coots — raph.coots@infoseller.com — }e%U8a4et — a94bf3fe2d8d393d786878d24844125e
Adona Wolters — adwolte@bing.com — 5ahu{Y&Y — 149c611ae4fcf33a508e49ce2ff36711
Thornley Maddy — thornlmadd@hotmail.com — !A-yQYRa%eZU — 940520d86157a88e0e7df57800232b60
Dobrila Covey — dobrilaco@msn.com — Qy(E{E& — dc9e14c0936b7b24a1707f174e9bc8fe
Jumoke Joslin — jumojo@carmag.com — XY8E)y? — 2613a8ec70109e9693f71268b84772c7
Kenelm Parada — kene_pa@yoohoo.com — ZESe]e^A — c344ee7b405c0920104feee232b6ba22
Reynold Matthias — reynol-matthi@linux.com — SAqY{AraH — bbe90a15208cb637585d20f889df7fee
Mackenzie Jeffrey — mackenzi.jeffr@bing.com — .U#uSaP — f148b6c2f066aca417a481daa0bf9928
Maye Benge — may.beng@mail.com — PUVuDYv — 25295ed546c9b7b8c5a6d54c6137af00
Keira Bushnell — keir_bushnell@infoseller.com — mY?e6y[a — 0af38a5227e462d602a24b6de58fcacb
Daryl Telford — daryltel@hotmail.com — @UtaQu~U&e — 4a99e5cdda46120c1c7bc65ab4791965
Ronalda Cheng — ronal_ch@freewebmail.com — ?EsU]Y2AJ — ead4dd13f871bd99e536c
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.
Thanks @Thomas1965, yeah it make sense to me, i will try that when i close from work.
i really appreciate your support.
Thanks for your support
it work now
Topic archived. No new replies allowed.