mail.dat code problem

I have no clue what is wrong with this code. The only output I get is the last email address from mail.dat, this is due tomorrow please help.

mail.dat =
From: sharon@marzipan.edu
Date: Wed, 13 Aug 2003 17:12:33 EDT
Subject: Re: hi
To: john@meringue.com
John,
Dave's email is dave_smith@icing.org
ttyl,
sharon

my only output is: dave_smith@icing.org
it needs to be:

The file Mail.dat contains the following e-mail addresses:
sharon@marzipan.edu
john@meringue.com
dave_smith@icing.org


#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
int pos;
int lens1;
string s1;
string email;
ifstream indata;
ofstream outdata;

indata.open("D:\\mail.dat");
outdata.open("D:\\list.dat");

if (!indata)
cout << "The file mail.dat cannot be opened" << endl;

if (!outdata)
cout << "The file list.dat cannot be opened" << endl;

if (indata.is_open())


{
while (!indata.eof()) // read the entire file including white space till the end of the file

indata >> s1;


pos = s1.find("@"); // find the "@" in the string

lens1 = s1.size(); // find the size of the string

if (pos <= lens1 && pos>lens1) // if the "@" character is within the measured string then cout as s1


cout << "The file Mail.dat contains the following e-mail addresses: " << endl;
cout << s1 << endl;
outdata << s1;
cin.get();
}
{
indata.close();
outdata.close();
}


return 0;
}
It's not a good idea to loop on eof()

There are a few other issues with the code too. The main loop can be much simpler (I've omitted the detailed cout etc. here.)
1
2
3
4
5
6
7
8
9
10
    string s1;
    
    while (indata >> s1)    // read the entire file one word at a time
    {
        size_t pos = s1.find("@"); 
        if (pos != string::npos)
        {
            // output the email address s1 here
        }
    }

It worked great! Thank you so much for your help!
Topic archived. No new replies allowed.