Thread: Reading postal address using cout

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    Reading postal address using cout

    I have a text file addresses.txt which contains a postal address as a single record
    i.e
    24 Church Street McClerance Close California 5084 .. record 1
    .................................................. ................................. record 2
    24 Church street is to be read to variable line1
    McClerance Close is to be read to variable line2
    California is to be read to variable city
    5084 is to be read to variable zip

    How do i read this at once to all these variables?

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    You should put ,'s in the address and read up to them as substrings. So you'd read between the square brackets then.
    [24 Church Street], [McClerance Close], [California], [5084]
    So you could then use getline and use ',' as a deliminator rather than '\n'

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by twomers View Post
    You should put ,'s in the address and read up to them as substrings. So you'd read between the square brackets then.So you could then use getline and use ',' as a deliminator rather than '\n'
    I used commas, and here's how am reading them, but i get a runtime exception on the first getline

    Code:
    std::istream& operator>>(std::istream& in, Address& address)
    {
    	char *fline="";
    	char *sline="";
    	char *tline="";
    	char *cty="";
    	char *cde="";
    	
    	in.getline(fline, 100, ','); /* This throws an exception */
    	in.getline(sline, 100, ',');
    	in.getline(tline, 100, ',');
    	in.getline(cty, 100, ',');
    	in.getline(cde, 5);
    
    	std::string firstln(fline), secondln(sline), thirdln(tline), city(cty), code(cde);
    	address.setAddress(firstln, secondln, thirdln, city, code);
    
    	/*
    	in >> fline;
    	in >> sline;
    	in >> tline;
    	in >> city;	
    	in >> cde;
    	*/
    
    	return in;
    }

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    http://cpwiki.sourceforge.net/Common...kes_and_errors
    Read especially the two first topics.
    Use std::string and std::getline.

    You also seem to have been poisoned by some members or some tutorials...
    C++ programmers should use T* and not T *!
    Last edited by Elysia; 01-04-2009 at 07:11 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    fline, sline tline .. are string literals that cannot be modified.
    Why don't you use the std::string version of getline ?
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing variables with memory address
    By ITAmember in forum C Programming
    Replies: 54
    Last Post: 06-28-2009, 03:35 AM
  2. How to Send Mac Address From Client to Server
    By Lieyza197 in forum C Programming
    Replies: 2
    Last Post: 05-27-2009, 09:58 AM
  3. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  4. What does this do (Windows API)?
    By EVOEx in forum Windows Programming
    Replies: 4
    Last Post: 12-19-2008, 10:48 AM
  5. reading hexadecimal variables
    By juhigarg in forum C++ Programming
    Replies: 7
    Last Post: 11-06-2001, 11:10 PM