I am trying to write a program that takes email addresses out of a text file (for easy copy paste) but when it finds one and prints it to the console it skips to the next line. So if there are two email addresses in one line it will only print the first one. I just threw in a cout in order to check it, I still have a few things to do but I've gotten stuck on this part, so there is missing pieces. It is for a class so if anyone has any input where I need to review it would be much appreciated. Attached is one of the files I am using to test.
Thank you.

while (true)
    if (!fin.good()) break;
    string lineFromFile;
    getline(fin, lineFromFile);

    for (int i = 0; i < lineFromFile.length(); i++) // for each char in the string... 
    if (lineFromFile[i] == '@')
    { // front and back traverse                      
      s = i;
      while (true)
        if (s < 0) break; 
        if (isValidEmailCharacter(lineFromFile[s]) == false) break;             
      int e = i;
      bool hasDot = false; 
      while (true)          
        if (e == lineFromFile.length()) break;
        if (isValidEmailCharacter(lineFromFile[e]) == false) break;
        if (lineFromFile[e] == '.') hasDot = true;

      if ( s < i && e > i && hasDot == true)
          anEmail = lineFromFile.substr(s, e-s);
          cout << anEmail << " ";
          // set aName's value
          if (nEmails < MAX_EMAILS)
          email[nEmails++] = anEmail;
    } // end front and back traverse         
  } // while

[valid02@email2.com not valid text goes here invalid@address

valid02@email3.com incomplete.com @ won't read this@invalid
.email valid04.valid04@email4.net valid05@email5.com* 

this file has 5 valid emails.
10 Years
Discussion Span
Last Post by Duoas

I'm sorry but I'm probably misunderstanding you. I changed that outside loop but I still can't get it to store and print more than email from one line. Would you mind clarifying it for me?
Thank you.


Here is how to extract all email addresses on a single line. It just keeps find the @ symbol until no more can be found. Once the @ symbol is found this program erases the address from the line and looks again for another @ symbol.

#include <iostream>
#include <string>
using namespace std;

int main()
    // list of all valid email characters.
    string chrs = "-_.abcdefghijklmnopqrstuvwxyz123456890";
    // a line that contains two email addresses and some other stuff
    // Assume the emails below are fake -- I don't know if they are real or not.
    string line = " here at cscgal@daniweb.com and here too happygeek@daniweb.com";
    size_t pos = 0;

    while( (pos = line.find('@') ) != string::npos)
        string st = line.substr(0,pos);
        line = line.substr(pos);
        for(size_t e = st.length()-1; e >= 0; e--)
            char c = st[e];
            if( chrs.find_first_of(c) == string::npos )
                st = st.substr(e+1);
        if( (pos = line.find(' ')) != string::npos)
            st = st + line.substr(0,pos-1);
            line = line.substr(pos+1);
            // end of line found, so just empty the string
            st += line;
            line = "";
        // print the email address
        cout << st << "\n";



It might be worth your interest to check out Boost RegExp (which is a lot easier to use than that scary-looking website might make you feel), or GNU RegExp.

Or, for a simple program you might want to check out Tcl/Tk, which excels at handling strings and has a powerful regexp.

If you are on *nix you can use utilities like gawk and grep to do the same.

Hope this helps.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.