Thread: Hey

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    8

    Hey

    Can anyone see what is wrong with this?

    insert
    Code:
         
    #include <string> 
    #include <fstream> 
    #include <iostream>
    #include <cstdlib>  
    #include <cctype>  
    #include <algorithm>  
    using namespace std;
    
    class toLower {public: char operator()(char c) const {return tolower(c);}}; 
    
    int main()
    { 
      const int MAX_EMAILS = 1000;
      int nEmails = 0;
      string email[MAX_EMAILS];
    
      string ifile; 
      cout << "Enter the name of the file to be opened: ";
      getline(cin, ifile);
      ifstream fin;
      fin.open(ifile.c_str());
      if(!fin.good()) throw "I/O error";
     
      if(ifile.empty())
        fin.open("fileContainingEmails.txt");
    
      string line;
      string anEmail;
      string a;
      string b;
      int i, s, e;
    
      while(getline(fin, line))
      {
        for (i = 0; i < line.length(); i++)     
        {
          if (line[i] == '@')
          {
            s = i - 1;
             if(s < 0) break;
             bool valid1 = false;
             if(line[s] >= 'a' && line[s] <= 'z')valid1 = true;
             if(line[s] >= 'A' && line[s] <= 'Z')valid1 = true;
             if(line[s] >= '0' && line[s] <= '9')valid1 = true;
             if(line[s] == '_' || line[s] == '+' || line[s] == '-')valid1 = true;
             if(valid1 == true)s--;
             if(vaild1 == false)break;
              
             e = i + 1;                
             if(e >= line.length()) break;
             if(line[e] >= 'a' && line[e] <= 'z')valid1 = true;
             if(line[e] >= 'A' && line[e] <= 'Z')valid1 = true;
             if(line[e] >= '0' && line[e] <= '9')valid1 = true;
             if(line[e] == '_' || line[e] == '+' || line[e] == '-')valid1 = true;
             if(valid1 == true)e++;
             if(vaild1 == false)break;
    	       
             bool hasdot = false;
             if(line[i] == '.')hasdot = true;
    
             if(s < i && i < e && hasdot)
             anEmail = line.substr(s, (e - s) + 1);
             a = anEmail;
             transform(a.begin(), a.end(), a.begin(), toLower());
    
             bool found = false;
             for (int i = 0; i < nEmails; i++) 
             {
               b = email[i];
               transform(b.begin(), b.end(), b.begin(), toLower()); 
               if(a == b)        
               if(found == true)break;  
             }
             if (!found && nEmails < MAX_EMAILS)
    		email[nEmails++] = anEmail;
          }
        }
      }
      
      fin.close();
    
      cout << endl;
      cout << nEmails << " email addresses were found in the input file" << endl; 
      cout << endl;
    
      ofstream fout;  
      if(nEmails > 0)
      {
        string ofile;
        cout << "Where are these email addresses to be sent: ";
        getline(cin, ofile);
        fout.open(ofile.c_str());
        for(i = 0; i < nEmails - 1; i++)    
        {       
         fout << email[i] << ";" << " " << endl;      
        }     
         fout << email[nEmails -1] << endl; 
        
        if(ofile.empty())
        fout.open("copyPasteMyEmails.txt");
        for(i = 0; i < nEmails - 1; i++)    
        {       
          fout << email[i] << ";" << " " << endl;      
        }     
          fout << email[nEmails -1] << endl; 
      }
         
      fout.close();
      return 0;
    }
    Last edited by mikecompsc; 05-24-2007 at 11:55 AM.

  2. #2
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    Code:
    if(ifile.length() < 0)
        fin.open("fileContainingEmails.txt");
    string::length() will never return anything less than 0.

    You probably want string::empty(). You did this in a few different places.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    8
    Thank you, do you see why I am having trouble extracting emails from a txt file? I not quite sure what im doing wrong.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Exactly what problems are you having? If you describe the problem it will be easier to look for the cause.

    BTW, this:
    Code:
      while(true)
      {
        getline(fin, line);
        if (!fin.good())break;
    would be better as this:
    Code:
      while(getline(fin, line))
      {
    By checking good, you might miss the last line in the file (good() can return false even if the getline read succeeds). Also, the second method is just cleaner.

    Your style is a little difficult to read as well. This makes it hard to identify potential problems by looking at the code. Consider putting the if control and the statement after it on separate lines.

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    8
    Thank you I will, the problem I am having is its not picking up the emails in the txt file, im pretty sure all the neccasary code is there. My only guess could be one of my loops is off but other than than that I dont know. I compiles like it should but dosent find the emails.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    All of this code
    Code:
                if(line[s] >= 'a' && line[s] <= 'z')valid1 = true;
                if(line[s] >= 'A' && line[s] <= 'Z')valid1 = true;
                if(line[s] >= '0' && line[s] <= '9')valid1 = true;
    could be written as
    Code:
    valid1 = isalnum(line[s]);
    Code:
                if(valid1 == true)s--;
                if(vaild1 == false)break;
    Why are you modifying s? It's not used beyond that point. Also, you could use an else there.

    But I think that this is your main problem:
    Code:
    if(found = true)break;
    You want ==. As written, it thinks that any email (except the first one) already exists, so they never get added.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    May 2007
    Posts
    8
    Thanks I must of some how I most of looked over that equal

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If you enable compiler warnings, the compiler should warn you about that sort of error. Enabling warnings differs from IDE to IDE, but you should be able to find something about it in the compiler options. For IDEs that use GCC or g++, you can add "-W -Wall" to the compiler options.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    May 2007
    Posts
    8
    Thank you I will, do you see why the cpp is not gathering the emails out of the txt file when complied?

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    What does it do?

    Code:
             if(s < i && i < e && hasdot)
             anEmail = line.substr(s, (e - s) + 1);
             a = anEmail;
             transform(a.begin(), a.end(), a.begin(), toLower());
    
             bool found = false;
             for (int i = 0; i < nEmails; i++) 
             {
               b = email[i];
               transform(b.begin(), b.end(), b.begin(), toLower()); 
               if(a == b)        
               if(found == true)break;  
             }
             if (!found && nEmails < MAX_EMAILS)
    		email[nEmails++] = anEmail;
    s < i and i < e will always be true because of how you set the variables. Also, because you don't have any braces {}, the if statement only applies to the anEmail= line. I think you should either wrap {}s around all of that code or have something like
    Code:
    if(hasdot == false) break;
    instead of the highlighted if statement.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User
    Join Date
    May 2007
    Posts
    8
    no its still not going, it most be worst than I thought. You wouldnt mind trying to compile this would you? I really need to get this done and I appreciate everyones help but i dont know what the problem is with this

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Could you be more descriptive about the problem? Not picking up the emails in the file doesn't really explain what is happening. What input are you giving? What output are you expecting? What output do you get? Be as descriptive as possible. Feel free to write an entire paragraph or three about it.

    Compiling and running doesn't help as much without the input file or an example of an input file that fails.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I can't compile anything on this computer, and even if I did, I don't have the data files that you're feeding your program.

    no its still not going, it most be worst than I thought.
    You need to be more specific. Does it add every line? Does it take the first email address and ignore all of the rest? Does it take only the first line? Does it segfault when you try to open the file?

    Code:
        for(i = 0; i < nEmails - 1; i++)    
        {       
         fout << email[i] << ";" << " " << endl;      
        }     
         fout << email[nEmails -1] << endl;
    That would crash if there were no emails.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Registered User
    Join Date
    May 2007
    Posts
    8
    im reading in a txt file that contains a staff directory and 40 emails. I name it read.txt It compiles then reads the file and says there is only 1 email but there is really 40. Then it prompts to know where to fout the output and in the outputed file there is nothing. Im sure its something small but i dont know what.

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    It compiles then reads the file and says there is only 1 email but there is really 40.
    Well then there's probably something wrong with the code that checks for duplicate emails, since the first email was correctly identified.

    This line in the for loop is useless, since any emails that have been added are already lowercased:
    Code:
    transform(b.begin(), b.end(), b.begin(), toLower());
    Code:
      fin.open(ifile.c_str());
      if(!fin.good()) throw "I/O error";
     
      if(ifile.empty())
        fin.open("fileContainingEmails.txt");
    Opening the file "" will likely fail, and so your code there would never get a chance to execute. I'd put it first and put the ordinary opening code in an else clause.

    [edit]
    Code:
    anEmail = line.substr(s, (e - s) + 1);
    That probably doesn't sdo what you want, as s is always equal to i-2 and e to i+2.

    You probably meant to put this code in a loop of some sort.
    Code:
            s = i - 1;
             if(s < 0) break;
             bool valid1 = false;
             if(line[s] >= 'a' && line[s] <= 'z')valid1 = true;
             if(line[s] >= 'A' && line[s] <= 'Z')valid1 = true;
             if(line[s] >= '0' && line[s] <= '9')valid1 = true;
             if(line[s] == '_' || line[s] == '+' || line[s] == '-')valid1 = true;
             if(valid1 == true)s--;
             if(vaild1 == false)break;
    Note that if you did the line in red would have to be executed only once. [/edit]
    Last edited by dwks; 05-24-2007 at 12:44 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hey, need help with Dropping ball and Bouncing
    By thynksheraze in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2003, 03:05 PM
  2. Hey
    By REMY in forum C++ Programming
    Replies: 1
    Last Post: 01-22-2003, 11:06 PM
  3. Hey all...
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 11-01-2002, 08:56 PM