Hey guys,
I'm kinda new at C++ and need help writing a program that finds email addresses in text files and stores them in an array. The email addresses are then filtered to make sure there are no duplicates. They are then copied to an output file.
The program should be designed as follows:
First ask the user for a name of a text file to input and the name of the output file. Open the input text file and load all of the text into one BIG string. Then process the string to find all email addresses. Store the email addresses in an array and then remove any duplicates. Finally write the unique email addresses to the output file.
I'm having two main problems so far. First of all, when I cout the string that stores the text of the file, only part of the text is outputted! Even though it was working earlier.
The second problem is the function call using .at. I planned to find the email addresses by searching for the "@" character and then making a loop that decreases character by character until an invalid character for an email address is found (such as a space). For some reason, this is not working.
Also, I do not know how to cin a filename.
Any help with these problems and additional assistance for this problem would be much appreciated :)
PLEASE point out any and all mistakes you find
Thanks in advance
Code:
// Program to read in a text file and output a list of unique email addresses contained within it in an output file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
bool isvalid(char);
void main ()
{
ifstream source, file1;
ofstream output, file2;
string bigstring, line;
string email1, email2, emailtest;
string emailads [100];
int i, j, n, pos1;
char c, x;
cout << "Please enter the name of the input and output files" <<endl;
cin >> file1 >> file2;
source.open(file1);
bigstring = "";
while (! source.eof())
{
source.get(c);
bigstring += c;
}
x = bigstring.at(j);
j = 0;
n = 0;
while (j < bigstring.length())
if (k = "@")
{
i = j;
pos1 = i;
//This is one of the main problems; I do not know why its not working:
while ((isvalid(bigstring.at(i)) == true))
{
i--;
}
email1.assign(bigstring,pos1-i,pos1);
i = pos1;
while ((isvalid(bigstring.at(i)) == true))
{
i++;
}
email2.assign(bigstring,pos1,pos1+i);
i = pos1;
emailtest = email1 + email2;
emailads [n] = emailtest;
n++;
}
source.close();
N = 100;
z=0;
for (int t = 0; t<N; t++) //Loop for controlling array element to be compared against
{
for (int k = t+1; k<N; k++) //Loop for controlling array element to be compared
{
if (emailads[t] != emailads[k]) //Checking matches of email addresses for each each array element
{
emailadsnew[z] = emailads[k]; //Storing unique email addresses in a new array
z++
}
}
}
ofstream output(file2);
for(int i=0;i<100;i++)
{
output<<emailadsnew[i]<< '\n' <<endl;
}
bool isvalid(char ch)
{
if
((ch >= 'a' && ch <= 'z') ||
(ch >= 'A' && ch <= 'Z') ||
(ch >= '0' && ch <= '9') ||
ch == '_' || ch == '-' || ch == '.' ||
ch == '!' || ch == '#' || ch == '$' ||
ch == '%' || ch == '&' || ch == '~' ||
ch == '*' || ch == '+' || ch == '-' ||
ch == '/' || ch == '=' || ch == '?' ||
ch == '^' || ch == '_' || ch == '`' ||
ch == '{' || ch == '|' || ch == '}' )
{
return true;
}
else return false;
}