help plz! find email in an .txt

hey everyone i am new to programming and have been working on this code for about two days with no solution. the code is supposed to find a email address withing a text file starting from the @ symbol back then to the end and find if it has a dot. as the code stands now all it dose is output blank lines. Any help will be appreciated thank you =)



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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Libraries
#include <fstream> 
#include <iostream> 
#include <string> 
#include <iomanip>
using namespace std;

// Compiler specific definitions
// None

// Programmer defined functions
struct userFile 
{ 
  string userInput;
  string userOutput;   
}; 

char line(string  line)                        
{
  char userSelection = ' ';
  int i = 0;
  //if sting isent empty
  if ( line.length() != 0){            
	  //loops every char looking for blanks
    while (i <  line.length()) 
	{
		//locates the postion
      userSelection=  line[i]; 
	  // if char isent blank
      if (userSelection != ' ') break; 
	  //goes to next char
      i ++;                             
    }
  }
  return userSelection;
}
//funtion to find input
userFile userInput(userFile& info)           
{
  string lines;                        
  //prompt the user
  cout << "Enter input file name [default: " << info.userInput << "]: ";       
  getline(cin, lines);   
  char userSelection = line(lines);    
  if (userSelection!= ' ')
  {                   
    info.userInput = lines; 
    info.userOutput = lines;     
  }  
  return info;   
}
//function find output file
userFile userOutput(userFile& info)            
{
  string lines;                          
   //prompt user
  cout << "Enter output file name [default: " << info.userOutput<< "]: ";       
  getline(cin, lines);                      
  char userSelection = line(lines);    
  if (userSelection != ' ')                  
    info.userOutput = lines;              
  return info;   
}

bool isValidEmailCharacter(char c)

{

bool result = false;

if (c >='A' && c <= 'Z' || c >= 'a' && c <= 'z'|| c >= '0' && c <='9' ||c =='.'||c =='-' || c =='+') result = true;

return result;

}
//prints out user userinput
void print(userFile& info)           
{
  cout << "Input file : "<< info.userInput << endl;
  cout << "Output file: "<< info.userOutput<< endl;
}

//Main program  
int main() 
{ 


  userFile info;
  //default for input
  info.userInput   = "fileContainingEmails.txt";   
  //default for output
  info.userOutput  = "copyPasteMyEmails.txt";      
 
  //gets input file from user
 userInput (info);  
  //gets outputfile from user
  userOutput(info);
  //opens selected files
  string line;
  ifstream fin;
 
  string lineFromFile;
  fin.open (info.userInput.c_str());
  if (!fin.good()) throw "File input error";
 


  const int MAX_EMAILS = 1000;
  int nEmails = 0;
  string email[MAX_EMAILS];

  while(true)
  {
    if (!fin.good()) break;
    char s;
   char e;
    string lineFromFile;
    getline(fin, lineFromFile);

  for (int i = 0; i < lineFromFile.length(); i++) // for each character in the string
  {
    
    if(lineFromFile[i] == '@')
      
    {

      for (s = i; s >= 0; s--)
      {
        if (!isValidEmailCharacter(lineFromFile[s]));

      for (e = i; e < lineFromFile.length(); e++)
      {
        if (!isValidEmailCharacter(lineFromFile[e]))
	
		{    
            if (nEmails > MAX_EMAILS)
			{
				email[nEmails++] == lineFromFile.substr(s, e-s);
		
			}
		     break;
	  }
        
  
      }
    }
  }
}
    for (int z=0;z<=nEmails;z++)
  {
  cout <<email[z] << endl;
  }
}

  print(info);   
cin.get();
  return 0; 
}
Last edited on
Topic archived. No new replies allowed.