input/ouput/ looping

Discussion in 'C' started by VickieQuirk, Jun 27, 2006.

  1. VickieQuirk

    VickieQuirk New Member

    Joined:
    Jun 27, 2006
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    I need help with the following assignment. I just can't seem to get it to work? It just creates a blank file for addresses.
    If anyone can tell me what I am doing wrong I will be forever grateful..

    Ok, here's the assignment:
    You're working for a company that's building an email list from files of mail messages. They would like you to write a program that reads a file called mail.dat, and that outputs every string containing the @ sign to file addresses.dat. For the purpose of this project, a string is defined as it is by the C++ stream reader - a contiguous sequence of non-whitespace characters.
    Here is what mail.dat has in it:
    ---------------------------------------------------------------------------
    From: kadcock@clevelandstatecc.edu
    Date: June 4, 2005

    To: Joe_student@tbrschool.edu

    Joe,

    Hope you have a good semester.

    Regards,
    Ken Adcock
    ---------------------------------------------------------------------------
    Here is what I have done:
    Code:
    #include <iostream>
    #include <fstream> 
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	
    	ifstream inFile;
    	ofstream outFile;
    	string tempString;
    	
    	
    	inFile.open("mail.dat");
    	outFile.open("addresses.dat");
    	
    	while(inFile)
    	{
    		inFile>>tempString;
    		if(tempString.find('@')!=string::npos)
    			outFile<<tempString;
    		
    	}
    	
    	inFile.close();
    	outFile.close();
    	return 0;
    }
     
    Last edited by a moderator: Jun 28, 2006
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    The problem is with
    inFile>>tempString;
    which does not allows the whitespaces and it just restricts itself to the first blank character and so does not fetch any email address in tempString. Try using getline instead.
     
  3. VickieQuirk

    VickieQuirk New Member

    Joined:
    Jun 27, 2006
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    My pleasure.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice