Thread: One Extra Input

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    85

    One Extra Input

    Hello,

    I am pulling in lines from a file. I am however getting one copy of the last record at the end...any suggestions

    Code:
    	fstream infile("hw5.data",ios::in);
    	info main_struct;
    	string line_string;
    	string fips;
    	string d_name;
    	string r_code;
    	int d_pop;
    	int sd_enrol;
    	int st_need;
    	
    		
    	while(infile)
    	{
    	        getline(infile,line_string,'\n');
                parse_string(line_string,fips,d_name,r_code,d_pop,sd_enrol,st_need);
    	        fips = "";
    	        d_name = "";
    	        r_code = "";
    	        d_pop;
    	        sd_enrol;
    	        st_need;
    	        cout << endl << endl << endl;
    	}

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Change this:
    Code:
    while(infile)
    {
      getline(infile,line_string,'\n');
    To this:
    Code:
    while(getline(infile,line_string,'\n'))
    {
    The eofbit for infile is only set after you fail to read input, so the loop will iterate once more than you want and line_string will have the same value as the last iteration, so it looks like you're reading the last line twice.
    My best code is written with the delete key.

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    The method Prelude just showed you is called a Priming Read.
    Sent from my iPadŽ

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    85
    Thank You this fixed it!!!!!!!!!

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    Code:
    fstream infile("hw5.data",ios::in);
    	info main_struct;
    	string line_string;
    	string fips;
    	string d_name;
    	string r_code;
    	int d_pop;
    	int sd_enrol;
    	int st_need;
    	
    		getline(infile,line_string,'\n'); //read one before loop
    	while(infile)
    	{
    	        
                parse_string(line_string,fips,d_name,r_code,d_pop,  sd_enrol,st_need);
    	        fips = "";
    	        d_name = "";
    	        r_code = "";
    	        d_pop;
    	        sd_enrol;
    	        st_need;
    	        cout << endl << endl << endl;
    
    getline(infile,line_string,'\n');
    	}
    I was taught to always read something before your loop, so this should also stop from reading and extra record

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Yes, this is another example of a priming read. It gets the same results as prelude's code, it just does is a bit more code.
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I was taught to always read something before your loop, so this should also stop from reading and extra record

    One disadvantage of this type of code is that you cannot use continue to skip the rest of the code block. For example, if you want to skip blank lines, you might check line_string.empty(), and continue if it is true. With the read at the end of the code block you will skip the read.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. input redirection
    By sashaKap in forum C Programming
    Replies: 6
    Last Post: 06-25-2009, 01:59 AM
  2. I would love some input on my BST tree.
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2007, 01:22 AM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  4. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM
  5. Help with Input Checking
    By Derek in forum C Programming
    Replies: 7
    Last Post: 06-17-2003, 03:07 AM