Hi,

I'm having trouble finding the '@' sign in multiple lines of text. What I want to do is find the @ sign, and if I have found it, print the whole line, if not, skip it.

So far I have this:

string input;
  getline(fin, input);

 for (int i = 0; !fin.eof( ); i++)      reading numbers
     {
          getline(file
          fin >> input;               
     }


  string s;
  string s2 ("@");
  size_t found;


for (int i = 0; i < s.find(s2); i++)
 { 

found=s.find(s2);
  if (found!=string::npos)
    cout << "first '@' found at: " << int(found) << endl;

}

I have no idea what I'm doing. Help me out please.

Thanks

Edited 6 Years Ago by __avd: Added [code] tags. Encase your code in: [code] and [/code] tags.

Delete line 2 -- it is more harm than good.

Replace all the rest of that code with this single loop. When the '@' is found I don't know what you want to do, so the code below will just display the entire text.

string line, check;
while( getline(fin, line) )
{
    check += line;
    size_t pos = check.find('@');
    if( pos != string::npos)
    {
        cout << check << '\n';
        check = "";
    }
}

Edited 6 Years Ago by Ancient Dragon: n/a

Thanks, that helped me a lot, I've been searching for hours and have not found a solution.

The problem now is that I need to validate that the '@' sign is part of an email. How would I do that?

I have this so far:

bool isValidEmailCharacter(char c) = false;
if (c >='A' && <='Z'|| c >='a' && <='z'|| c >='0' && <='9' || c =='.' || =='-' || =='+'.)
     { isValidEmailCharacter = true;

      }

 while (fin.good())
  {
     
    string line, check;

   while(getline(fin, line))
      {

      check += line;
      size_t pos = check.find('@');
      
   if( pos != string::npos)

      {         
         isValidEmailCharacter;
      } 
      } 
  }

But I am unable to compile it saying:

email12.cpp(15) : error C2072: 'isValidEmailCharacter' : initialization of a fun
ction
email12.cpp(15) : error C2440: 'initializing' : cannot convert from 'bool' to 'b
ool (char)'

Edited 6 Years Ago by __avd: Added [code] tags. Encase your code in: [code] and [/code] tags.

Are you attempting to write a function names isValidEmailCharacter() ? If yes, than what the hell is that "= false" doing after it on line 1 ? and where are the { at the start of the function and } at the end?

Line 3 doesn't work because you don't set the return value of a function like that.

bool isValidEmailCharacter(char c)
{
    bool ok = false;
    // function code does here


   return ok;
}
for (int i = 0; !fin.eof( ); i++)      reading numbers
     {
          getline( fin ,input);
          if(input.find("@")!=string::npos)
             printf("%s\n",input);
               
     }

Hi,

I'm having trouble finding the '@' sign in multiple lines of text. What I want to do is find the @ sign, and if I have found it, print the whole line, if not, skip it.

So far I have this:

string input;
  getline(fin, input);

 for (int i = 0; !fin.eof( ); i++)      reading numbers
     {
          getline(file
          fin >> input;               
     }


  string s;
  string s2 ("@");
  size_t found;


for (int i = 0; i < s.find(s2); i++)
 { 

found=s.find(s2);
  if (found!=string::npos)
    cout << "first '@' found at: " << int(found) << endl;

}

I have no idea what I'm doing. Help me out please.

Thanks

I want to write what I found in the file input to an output file, but it will not work. Why is this so?

ifstream fin;
  string fileName; 
  cout << "Enter input filename";

  getline(cin, fileName);

  if (fileName.length() > 0)
    {

        fin.open(fileName.c_str());
    }

  else 
     fin.open("input.txt");

     if (!fin.good()) throw "I/O error";
 


     while (true)
       {
           if (!fin.good()) break;

           string line, check;
            getline(fin, line);

            {

                check += line;
                size_t pos = check.find('@');
      
           if( pos != string::npos)

              {
 
  
                      cout << check << '\n';
                      check = "";
              }

           }
       }

  ofstream fout;
  fout.open("emails.tt");
  if (!fout.good()) throw "I/O error";

    fout << check << endl;
    fout.close();

Is the identifier I used in fout wrong? It says check is an undelcared identifier..

Edited 6 Years Ago by __avd: Added [code] tags. Encase your code in: [code] and [/code] tags.

- Did you include <fstream> ?
- Where's the rest of the code ?
- Why did you choose to change Ancient's code?

>>I want to write what I found in the file input to an output file, but it will not work. Why is this so?

Because you blanked it out on line 38, before it was written to the file. Move lines 44-49 to line 38 (just before blanking out check).

is this an assignment for a C++ class? or something you're really trying to do in real life?

if this is a real application, I'd say you're using the wrong language and you should seriously consider Perl. Perl was made for this problem.

you'd have a complete solution that parses validated email addresses from a file in about 4 or 5 total lines of code.


.

Edited 6 Years Ago by jephthah: n/a

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