0

This can be deleted if it should not be here, I'm new.

I'm trying to take the username and domain of an email address and print it as the following:

domain, username

However, when I run the following code:

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

using namespace std;

void parseTheEmail(string email, string &username, string &domain){
   username = "";
   domain = "";
   int atPosition = email.find('@');
   username = email.substr(0, atPosition);
   domain = email.substr(atPosition+1, -1);

   return;
}

int main(){
   string email, username, domain;
   string filename;
   cout << "Please enter a filename> ";
   cin >> filename;
   cout << endl;
   ifstream files;
   files.open(filename);

   while(!files.eof()){
      files >> email;
      parseTheEmail(email, username, domain);
      cout << domain << ", " << username << endl;
   }
   files.close();

   return 0;
}

It prints the last email out twice and I can't figure out why. For example, if I had:

stanford.edu, john
stack.com, mike
google.com, bob

It would print google.com, bob twice.

Why is this?

0 Answers0