problems with getline in a while loop

This is a discussion on problems with getline in a while loop within the C++ Programming forums, part of the General Programming Boards category; My program is suposed to input infromation from two files than calculate the taxes, and print out the info on ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    2

    problems with getline in a while loop

    My program is suposed to input infromation from two files than calculate the taxes, and print out the info on the screen. The problem is when I try to input information from one of my files it will run through the while loop and take in the getline, but it won't do it again, and again like it's supposed to. It just keeps cycling the loop, without running the getline.



    ************************************************** *
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    int main()
    {
    	string customerf = " ", customerl = " ", state = " ", statetax = " ";
    	int times;
    	double amount, paid, rate, total = 0;
    
    blah... 
    blah...
    blah...
    
    	
    	getline(instatetax, statetax, '#');
    	instatetax >> rate;
    
    		while (statetax != state)
    		{
    			getline(instatetax, statetax, '#');
    			instatetax >> rate;
    		}
    
    blah...
    blah...
    blah...
    
    
    Input file looks like
    
    AL#.056
    AK#.095
    AZ#.O625
    AR#.045
    CA#.08
    CO#.0725
    CT#.0775
    DE#.O7
    FL#.0725
    GA#.055
    HI#.0875
    ID#.06
    IL#.065

  2. #2
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    >It just keeps cycling the loop, without running the getline.
    That sounds like your loop condition isn't testing the right thing. Try printing out statetax with each iteration and see if it takes the line break with it. I'll bet you that it does, and state doesn't have a newline in the string, and your test is failing.
    Code:
    while (statetax != state)
    {
      getline(instatetax, statetax, '#');
      cout << '|' << statetax << "|\n";
      instatetax >> rate;
    }
    By the way, because the loop doesn't take into account end-of-file or stream errors, if the test fails, you'll loop forever. Aren't iostreams fun?

    Cheers!
    Last edited by Slacker; 12-14-2005 at 11:32 AM.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    2
    If i use that code it continually prints out " AZ "
    Which is the same thing I got when I used the "step into debugger"
    If the entire code is needed I can post it.
    But to my knowledge everything else is fine.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    There is an 'O' (capital 'o') in your input data after AZ. There are actually several in that input data. If that is not a typo, then it is what is causing your problem, because you are trying to read in a double, but .O is not a number. That puts the stream into a fail state and you never read in anything else.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual Studio Express / Windows SDK?
    By cyberfish in forum C++ Programming
    Replies: 23
    Last Post: 01-22-2009, 01:13 AM
  2. Problems with getline
    By MarlonDean in forum C++ Programming
    Replies: 5
    Last Post: 07-01-2008, 05:04 AM
  3. syntax question
    By cyph1e in forum C Programming
    Replies: 19
    Last Post: 03-30-2006, 11:59 PM
  4. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 07:26 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 08:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21