Thread: Help with loop

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    4

    Unhappy Help with loop

    The while loop does an extra iteration, and I don't know why. E.g. if there are only 2 lines in the file it is reading, it prints out the last price and quantity twice, and adds their product to sub. I think it prints out whitespace before that.

    fin is an ifstream object. If I change the while condition to (!EOF), the file is not read at all.

    I suspect that it has something to do with getline (fin, prodDesc), but prodDesc is a string that may contain spaces, so I don't know how else to read it in.

    Any suggestions? My head is swimming.

    -->Mariana

    Code:
    //this is part of a program that reads from a file
    // and outputs a billing invoice
    
    void processOrder(float&sub)
    	
    	{
    	
    	sub=0;
    	
    	do
    	{
    		fin>>code>>price>>quantity;
    		getline (fin, prodDesc);
    		total = price * quantity;
    		cout<<fixed<<showpoint<<setprecision(2)
    			<<setw(20)<<left<<code
    			<<setw(20)<<left<<prodDesc
    			<<setw(15)<<price
    			<<setw(15)<<quantity
    			<<setw(15)<<total;
    		sub += total;
    	}while(fin);
    
    	cout<<setw(30)
    		<<"Total order: $"
    		<<setprecision(2)
    		<<setw(15)
    		<<fixed<<showpoint
    		<<sub<<endl;
    	
    	}

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    A variation of the eof() problem discussed in the FAQ

    Try
    Code:
    while ( fin>>code>>price>>quantity ) {
    	getline (fin, prodDesc);
    	total = price * quantity;
    	cout<<fixed<<showpoint<<setprecision(2)
    		<<setw(20)<<left<<code
    		<<setw(20)<<left<<prodDesc
    		<<setw(15)<<price
    		<<setw(15)<<quantity
    		<<setw(15)<<total;
    	sub += total;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2004
    Posts
    4

    Cool

    Thank you!

    I actually asked a friend for help, and we ended up using the return value of getline() to break it up.

    Code:
    //Reads from file, calculates subtotal
    void processOrder(float&sub)
    	
    	{
    	
    	sub=0;
    	
    	do
    	{
    		fin>>code>>price>>quantity;
    		if (getline (fin, prodDesc) != 0)
    		{
    		total = price * quantity;
    		cout<<fixed<<showpoint<<setprecision(2)
    			<<setw(10)<<left<<code
    			<<setw(20)<<left<<prodDesc
    			<<setw(10)<<right<<price
    			<<setw(15)<<quantity
    			<<setw(15)<<total<<endl;
    		sub += total;
    		}
    	}while(fin);
    
    	cout<<setw(55)
    		<<"Total order: $"
    		<<setprecision(2)
    		<<setw(15)
    		<<fixed<<showpoint
    		<<sub<<endl;
    	
    	}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM