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

sol. can be posted in 2 parts Problem 5. You\'re working for a company that\'s b

ID: 3612628 • Letter: S

Question

sol. can be posted in 2 parts Problem 5. You're working for a company that's building an email listfrom files of mail messages. They would like you to write a programthat reads a file called mail.dat. and outputs every string containing the@ sign to file addresses.dat. Forthe purpose of this project, a string is defined as it is by theC++ stream reader-a contiguous sequence of non-whitespacecharacters. Given the data: From: sharon@marzipan.edu Date: wed, 13 Aug 2003 17:12:33EDT Subject: Re: hi To : john@meringue.com John, Dave's email is dave_smith@icing.org. ttyl. sharon Then the program would ouput on file addresses.dat: sharon@marzipan.edu
john@meringue.com
dave_smith@icing.org. Use meaningful variable names, proper indentation, andappropriate comments thoroughly test the proram using your datasets. sol. can be posted in 2 parts Problem 5. You're working for a company that's building an email listfrom files of mail messages. They would like you to write a programthat reads a file called mail.dat. and outputs every string containing the@ sign to file addresses.dat. Forthe purpose of this project, a string is defined as it is by theC++ stream reader-a contiguous sequence of non-whitespacecharacters. Given the data: From: sharon@marzipan.edu Date: wed, 13 Aug 2003 17:12:33EDT Subject: Re: hi To : john@meringue.com John, Dave's email is dave_smith@icing.org. ttyl. sharon Then the program would ouput on file addresses.dat: sharon@marzipan.edu
john@meringue.com
dave_smith@icing.org. Use meaningful variable names, proper indentation, andappropriate comments thoroughly test the proram using your datasets.

Explanation / Answer

#include<iostream.h>
#include <fstream.h>
#include <string>
using namespace std;
int main()
{int j,last,first,found,i;
string input;
ifstream in;
ofstream out;

in.open("mail.dat");   
if(in.fail())            //is it ok?
       { cout<<"input file didnot open please check it ";
        system("pause");
        return 1;
        }
  out.open("addresses.dat");          //open file
  if(out.fail())            //is it ok?
       { cout<<"output file didnot open please check it ";
        system("pause");
        return 1;
        }
out<<"The e-mail addressesare:"<<endl;       
getline(in,input);  
while(in)       
   {j=0;
   last=input.length();
   first=0;
   found=0;
    while(j<input.length())
       {if(input[j]=='@')
          found=1;
        else
           if(input[j]==' ')
               if(found==1)
                  {last=j;
                   j=input.length()+5;
                  }
               else
                  first=j+1;                 
        j++;
       }
       if(found==1)
         {for(i=first;i<=last;i++)
             out<<input[i];
         out<<endl;
         }               
    getline(in,input);           
    }
in.close();
out.close();
system("pause");
return 0;
}