Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ subString POS
    primarykey
    data
    text
    <p>I have written a program that looks for valid email addresses in a text file that has many emails both valid &amp; invalid.</p> <p>&amp; then outputs the vaild emails in another text file.</p> <p>The problem i have is creating a substring or object that will read the line left of the @ symbol &amp; read true if there are multiple '.'</p> <p>for example: read this line of text as a valid email - Jimmy.Cole@yahoo.com</p> <p>&amp;</p> <p>if there are more '.' after the @ symbol read as false. here is my code: </p> <pre><code>#include&lt;iostream&gt; #include&lt;string&gt; #include&lt;fstream&gt; using namespace std; const string DEFAULT_INPUT_FILENAME="fileContainingEmails.txt"; const string DEFAULT_OUTPUT_FILENAME="copyPasteMyEmails.txt"; const int N=1000; //assuming atmost 1000 email //function prototypes bool isNotDuplicateEmail(string emails[], int emailCount, string emailTemp); bool isValidEmail(string emails[], string emailTemp); void WriteToFile(string emails[], int emailCount, string outputFile); void DisplayEmailsToConsole(string emails[], int emailCount); bool isValidEmailChar(char c); int countOccurenceOfAt(string emailTemp); int countOccurenceOfDot(string emailTemp); int main() { //variable declaration ifstream fin; string inputFile; string outputFile; int emailCount=0; string emails[N]; //ask user for input and output file name cout&lt;&lt;"Enter input file name: "; getline(cin,inputFile); cout&lt;&lt;"Enter output file name: "; getline(cin,outputFile); //check for default names if(inputFile=="") inputFile=DEFAULT_INPUT_FILENAME; if(outputFile=="") outputFile=DEFAULT_OUTPUT_FILENAME; cout&lt;&lt;"Input File Name = "&lt;&lt;inputFile&lt;&lt;endl; cout&lt;&lt;"Output File Name = "&lt;&lt;outputFile&lt;&lt;endl; //open input file fin.open(inputFile.c_str()); //if file does not exists, display error message and return if(!fin) { cout&lt;&lt;"ERROR: "&lt;&lt;inputFile&lt;&lt;" doen not exists."&lt;&lt;endl; return 1; } string emailTemp; /*read each line from file, and if it is not a duplicate email and a valid email then add it to email list and increment count*/ while(getline(fin,emailTemp)) { if(isNotDuplicateEmail(emails,emailCount,emailTemp) &amp;&amp; isValidEmail(emails,emailTemp)) { emails[emailCount]=emailTemp; emailCount++; } } //close input file fin.close(); //call function to write emails to output file and console if(emailCount&gt;0) { WriteToFile(emails, emailCount, outputFile); cout &lt;&lt; endl &lt;&lt; emailCount &lt;&lt; " email addresses were found, and copied to the file " &lt;&lt; outputFile&lt;&lt;endl; DisplayEmailsToConsole(emails, emailCount); } else { cout &lt;&lt; "Sorry, no email addresses were found in the file " &lt;&lt; inputFile &lt;&lt; endl; } cout &lt;&lt; endl&lt;&lt; "Press ENTER to continue..." &lt;&lt; endl; cin.get(); return 0; } bool isNotDuplicateEmail(string emails[], int emailCount, string emailTemp) { /*Check each email address and if a duplicate email is found, return false*/ /*If email is not a duplicate email, return true*/ for(int i=0;i&lt;emailCount;i++) { if(emailTemp==emails[i]) return false; } return true; } bool isValidEmail(string emails[], string emailTemp) { if(emailTemp=="") return false; /*If there are more than 1 @*/ if (countOccurenceOfAt(emailTemp)!=1) return false; /*If there are more than 1 .*/ if(countOccurenceOfDot(emailTemp)!=1) return false; int index1=-1; int index2=-1; //check each character for valid email char for (int i=0; i&lt;emailTemp.size(); i++) { if(!isValidEmailChar(emailTemp[i])) return false; } //find index of @ and . index1=emailTemp.find_first_of('@'); index2=emailTemp.find_first_of('.'); if(index1&lt;1) return false; if(index2&lt;index1) return false; if((index2-index1)==1) return false; if(((emailTemp.size()-1)-index2)==0) return false; return true; } bool isValidEmailChar(char c) { /* check for valid email characters 0-9, @, ., A-Z, a-z, _, +, - */ if(c&gt;='0' &amp;&amp; c&lt;='9') return true; if(c&gt;='A' &amp;&amp; c&lt;='Z') return true; if(c&gt;='a' &amp;&amp; c&lt;='z') return true; if(c=='@' || c=='.' || c=='_' || c=='+' || c=='-') return true; return false; } /*Function to count occurence of @ in email address*/ int countOccurenceOfAt(string emailTemp) { int count=0; for(int i=0;i&lt;emailTemp.size();i++) { if(emailTemp[i]=='@') count++; } return count; } /*Function to count occurence of . in email address*/ int countOccurenceOfDot(string emailTemp) { int count=0; for(int i=0;i&lt;emailTemp.size();i++) { if(emailTemp[i]=='.') count++; } return count; } void DisplayEmailsToConsole(string emails[], int emailCount) { /*Display console message and then display each email address, one at a line*/ cout &lt;&lt; endl &lt;&lt; endl &lt;&lt; "You can open the output file and copy/paste its contents into the \"to\", \"cc\", or \"bcc\" field of any email message. It is best to use the \"bcc\" field so that everyone's email address does not appear in the message, to protect their privacy"&lt;&lt;endl; cout &lt;&lt; endl &lt;&lt; "Email List is: "&lt;&lt; endl; for (int i=0; i&lt;emailCount; i++) { cout &lt;&lt; emails[i] &lt;&lt; endl; cout &lt;&lt; endl; } } void WriteToFile(string emails[], int emailCount, string outputFile) { /*Open file and write each email address to output file followed by ; */ ofstream fout; fout.open(outputFile.c_str()); for(int i=0; i&lt;emailCount; i++) { fout &lt;&lt; emails[i] &lt;&lt; "; " &lt;&lt; endl; } //close file fout.close(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload