Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. Write email3.cpp from email3-algorithm.txt Email addresses consist of the cha

ID: 3771201 • Letter: 2

Question

2. Write email3.cpp from email3-algorithm.txt 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 at least one '.'. We are only using the definitions highlighted in green on this site: http://www.remote.org/jochen/mail/info/chars.html The program should prompt the user for the input and output filenames, open and read the input file, and output to the console the valid email addresses as they are found. If a line contains the character @, create the variables s, e, and hasDot, and use loops to find their values, per the lectures. If the values for these three indicate that the @ is inside a valid email address, output to the console that address. Repeat for each @ in a line. Do not deal with lists or duplicates at this point. Here are some guidelines: If you open the output file, you are not doing this right. If your e-loop is NOT after your s-loop, you are not doing this right. If your s- and e-loops are not contained in the code block of an if-statement that tests for the character @, you are not doing this right. If you do not using the substring function, you are not doing this right. If you do not create your own input files with which to test your program, you are not doing this right. If you do not test your program with the ten .txt files provided in the project writeup, and see duplicate email addresses outputed, you are not doing this right. Compile and run the program. Submit the source file to the class website for credit. Example. Your program's console I/O should look something like this, excluding your identifying information: Enter input filename [default: fileContainingEmails.txt]: x.txt Enter output filename [default: x.txt]: y.txt Input file name is x.txt Output file name is y.txt Please press Enter key to continue: RBurns@dvc.edu RBurns@dvc.edu Number of valid email addresses is 2

Explanation / Answer

Code

#include <algorithm>
#include <deque>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
#include <cctype>
class mytoLower
{
   public:
   char operator()(char c) const
   {
       return tolower(c);
   }
};
string defaultInFile = "input.txt";
/* default inputFile */
string inFile;
string defaultOutFile = "output.txt";
/* default outputFile */
string outFile = defaultOutFile;
string linesFromFile;
int emailPrintCount = 0;
void getInput()
/* prompting the user for name of inputfile */
{
   cout << "Enter filename of input file (default = "" << defaultInFile << ""):      ";
   getline(cin, inFile);
   if (inFile == "")
       inFile = defaultInFile;
   if (inFile != defaultInFile)
       outFile = inFile;
}
void getOutput()
/* prompting the user for name of outputfile */
{
   string tmpOut;
   cout << "Enter filename of output file (default = "" << outFile << ""):      ";
   getline(cin, tmpOut);
   if (tmpOut != "") outFile = tmpOut;
}
bool testValidity(string linesFromFile, int charAt)
{
   bool havingDot = false;
   int tmpCharAt = charAt+1;
   for (int i = 0; i < (linesFromFile.length() - charAt); i++)
   {
       if (linesFromFile[tmpCharAt] == '.') havingDot = true;
       else if (linesFromFile[tmpCharAt] == ' ' || linesFromFile[tmpCharAt] == ',' ||
           linesFromFile[tmpCharAt] == ';' || linesFromFile[tmpCharAt] == '@') break;
           tmpCharAt++;
           /* incrementing forward 2 next character & check */
   }
   if (linesFromFile[charAt - 1] == '.' || linesFromFile[charAt - 1] == '@' ||
       linesFromFile[charAt - 1] == ' ') havingDot = false;
       return havingDot;
}
struct ValidEmails
{
   string validEmails;
};
bool checkDuplicates(string lowerEmails, deque<ValidEmails>& finallowerEmails)
{
   bool isDuplicate = false;
   for (int i = 0; i < finallowerEmails.size(); i++)
   {
       if (finallowerEmails[i].validEmails == lowerEmails)
           isDuplicate = true;
   }
   /* for loop - checking against the lowercase case */
   return isDuplicate;
   /* returning the boolean resulting */
}
int main()
{
   deque<ValidEmails> finalEmails;
   /* List ValidEmails */
   deque<ValidEmails> finallowerEmails;
   /* for the duplicates checking */
   /* Declare the needed variables */
   ifstream finput;
   ofstream foutput;
   getInput();
   getOutput();
   cout << endl << "   File Input Name = " << inFile << endl;
   cout << "   File Output Name = " << outFile << endl << endl << endl;
   finput.open(inFile);
   if (!finput.good()) throw "I/O error";
   while (true)
   {
       if (!finput.good()) break;
       getline(finput, linesFromFile);
       for (int i = 0; i < linesFromFile.length(); i++)
           if (linesFromFile[i] == '@')
           {
               int charAt = i;   
               if (testValidity(linesFromFile, charAt) == true)
               {
                   string email = "";
                   for (int s = charAt; s>=0; s--)
                   {
                       if (linesFromFile[s] == ' ' || linesFromFile[s] == ',' ||
                       linesFromFile[s] == ';' || linesFromFile[s] == '   ' ||
                       linesFromFile[s] == ':' || linesFromFile[s] == '=' ||
                       linesFromFile[s] == '>' || linesFromFile[s] == '<' ||
                       linesFromFile[s] == '/' || linesFromFile[s] == '\' ||
                       linesFromFile[s] == '(' || linesFromFile[s] == ')' ||
                       linesFromFile[s] == ']' || linesFromFile[s] == '[' ||
                       linesFromFile[s] == '{' || linesFromFile[s] == '}') break;
                       email = linesFromFile[s] + email;
                   }
               for (int e = charAt+1; linesFromFile[e-4] != '.'; e++)
               {
                   email = email + linesFromFile[e];
               }
               string lowerEmails = email;
               transform(lowerEmails.begin(), lowerEmails.end(), lowerEmails.begin(), mytoLower());
               if (checkDuplicates(lowerEmails, finallowerEmails) == false)
               {
                   ValidEmails aFinalEmails;
                   aFinalEmails.validEmails = email;
                   finalEmails.push_back(aFinalEmails);
                   cout << email << endl;
                   aFinalEmails.validEmails = lowerEmails;
                   finallowerEmails.push_back(aFinalEmails);
                   emailPrintCount++;
               }
           }
       }
   }
   finput.close();
   if (emailPrintCount == 0) cout << "There are no e-mails in this file." << endl;
       return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote