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;
	 
}

Edited 6 Years Ago by Nick Evan: Added code-tags

Regarding the filenames, this won't work:

ifstream source, file1;
ofstream output, file2;

cout << "Please enter the name of the input and output files" <<endl;
cin >> file1 >> file2;
source.open(file1);

I'm going to rename file1 and file2 to filename1 and filename2 to make the names more clear. I am also going to make them strings, which is where the problem is:

ifstream source;
string filename1;
ofstream output;
string filename2;

cout << "Please enter the name of the input and output files" <<endl;
cin >> file1 >> file2;
source.open(filename1.c_str ());

Look closely at the error messages. They should tell you that the parameters you are using aren't what the function requires. Then look up the function and see what it needs.

http://www.cplusplus.com/reference/iostream/ifstream/open/

void open ( const char * filename, ios_base::openmode mode = ios_base::in );

It wants a const char* , not an ifstream. I have filename declared as a string, so I needed to convert it, which is why I used this:

http://www.cplusplus.com/reference/string/string/c_str/

const char* c_str ( ) const;

Here's one of the main stopping places for any errors you get. Look up the function, see what library it's from, see what it returns, and see what parameters are required. It also has examples.

http://www.cplusplus.com/

Edited 6 Years Ago by VernonDozier: n/a

Thanks a lot Vernon,
I've made edits and fixed most of the errors. But theres still a runtime error. I think I've isolated it at line 62 but I don't know how to fix it. Could you help me out?

// 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;
	ofstream output;
	string file1,file2;
	string bigstring, line;
	string email1, email2, emailtest;
	string emailads [100], emailadsnew[100];
	int i, n, N, z, pos1;
	unsigned int j;
	char c;

	cout << "Please enter the name of the input and output files" <<endl;
	cin >> file1 >> file2;

	source.open(file1.c_str()); 
	output.open(file2.c_str());
	bigstring = "";
	
	while(source.fail())
	{
		cout<<"Error: Invalid input file name. Please make sure the file is in the default directory or provide full path."<<endl;
		cout<<"Please reenter input file name: ";
		cin>> file1;
		source.close();
		source.clear();
		source.open (file1.c_str());
	}

	while (! source.eof())
	{      
	source.get(c); 
	bigstring += c;
	}

	
	
	n = 0;

	for (j = 0; j < bigstring.length(); j++)

	if (bigstring.at(j) = '@')
	{   

		i = j-1;
		pos1 = i;
		
		while ((isvalid(bigstring.at(i)) == true))
		{
			i--;
		}
	
		email1.assign(bigstring,i,pos1);

		i = pos1+1;
		
		
		while ((isvalid(bigstring.at(i)) == true))
		{
			i++;
		}

		email2.assign(bigstring,pos1,i);
		
		
		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++; 
				}	
		}
	}

 
    for(int i=0;i<100;i++)
    {
        output << emailadsnew[i] << '\n' <<endl;
    }
	
	output.close();
}

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;
	 
}

Edited 6 Years Ago by som3aman: Personal information

Line 62 is a bracket, so that's not the problem. You're going to have to be more specific about what the error is. Segmentation fault? Divide by zero? Does it always occur? Only sometimes? What's the exact line number? What is the exact error message? What is the input? What's the output? What are the variable values when the error strikes? Etc.

But it's not in line 62 above. Possible culprits might be bad index values on strings.

string aString = "house";
char aChar = aString[-1]; // error
char aChar2 = aString[5]; // error

But we need more details and the exact line number and exact error message.

This article has been dead for over six months. Start a new discussion instead.