i am currently new to c++ coding and i have been working on this code for a class, i am suppose to get emails from a .txt file and the program reads each line of the file analyzing each and every char of every line to see if it is a valid email address. for example: [email]johndoe@yahoo.com[/email] = valid, [email]john@doe@yahoo.com[/email] = invalid.

right now im having trouble getting the program to process each line and print to the console the valid email address only.

any help would be great. thanks.

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

int main()
{
  ifstream fin;
  ofstream fout;

  string line;
  string inputemail;
  cout << "Enter input filename [default: fileContainingEmails.txt]: ";
  getline(cin, inputemail);
  fin.open(inputemail.c_str());

  cout << endl;

  string Emails;
  int i;
  while (getline(fin, Emails))
  {
      for (i = 0; i < Emails.length(); i++)
      {
          if (Emails[i] == '@')
          {
              cout << Emails[i] << endl;
          } // if
      } // for
  } // while


  cout << endl;



  // output file section
  string outputemail;
  cout << "Enter output filename [default: copyPasteMyEmails.txt]: ";
  getline(cin, outputemail);
  fout.open(outputemail.c_str());


  fin.close();
  fout.close();

  cout << endl;
  cout << "Input File: " << inputemail << endl;
  cout << "Output file: " << outputemail << endl;

  return 0;
}

Recommended Answers

All 6 Replies

You are looking for the @ and printing the address as soon as you have found it.

What you need to do is count the number of @ in the string then once the loop has finished if and only if you have exactly 1 @ print the address.

It seems to me a common error of inexperienced programmers when using a loop to perform some test to try to put the if conditional for success inside the loop. However there are many times, such as this, when the actual result comes from running the entire loop because the result is not dependent on the value of a single entry in the array but rather on the state of the array as a whole.

You use the loop to calculate that entire state then test the state after the loop has finished executing.

yes i am looking for the @ sign but also if the sections left and to the right of the @ sign are valid also.

and if code could possibly be put up to show examples, that would be greatly appreciated.

well i figured out how to print the emails with @ signs but now i cant seem to print only the emails with 1 @ sign, i can not figure out how to eliminate emails that have more then one @ sign.

I already told you that in my first post. As to validating the rest of the address there isn't much you can do.

You can check that only valid characters are used and that length restrictions are not exceeded. You can find some of the details required here

In the programming languages like PHP or Java this function is
constructed by using the `regex`.But I don't recomand to use regex
because they not constant time algorithms.And yes there are regex
libraries for C++ or even using a lex.using lex you can write your own parser.

But let's go to a simpler method.
You can just code a state machine, Just google about state machines (DFA)
deterministic finite automata and then you'll get what is a finite machine.
Then draw the state machine and code it yourself.

Member Avatar for iamthwee

But let's go to a simpler method.
You can just code a state machine, Just google about state machines (DFA)
deterministic finite automata and then you'll get what is a finite machine.
Then draw the state machine and code it yourself.

While this is true, I think that the expense to time ratio for regex is perfectly OK. You could do this in one line using regex boost libraries, if you really wanted to.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.