Thread: Break statement issues, how to return to start of loop?

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    1

    Break statement issues, how to return to start of loop?

    I have a loop that was executing in infinity. Now my issue is that I have included break statements for each container size, but this will stop the loop. I have tried to return to the start of the loop, but it didn't seem to change anything. I have also tried to use a 'do' loop and incremently count with a 'for', but that didn't seem to work either. Does the statement 'dooflingy -= 50;' make sense to act like a counter? When executed the container size will increment only once, i.e. 55 is entered the huge size will increment 1, but the medium will not count.
    Code:
    /*Purpose:  Read number of Dooflingies to be shipped and display
    **	    the number of huge, large, medium, & small containers
    **	    needed to send the shipment in the minimum number
    **	    of containers and with the minimum amount of wasted space.
    **Input:	    Enter Dooflingy totals to be shipped.
    **Output:   Display a column of 'Container' with the seperate container 
    **	    size limits huge = 50, large = 20, medium = 5, small = 1.
    **	    A second column 'Number' will display the container size amounts.
    */
    
    #include <iostream>		//cin, cout, <<, >>
    
    using namespace std;
    
    
    //start of main
    int main()
    {
         int huge = 0, large = 0, medium = 0, small = 0, dooflingy = 0;
    
         //input dooflingies to be shipped
         cout << "\nEnter the number of Dooflingies to be shipped: "; 	
         cin >> dooflingy;
         cout << "\nYou entered " << dooflingy << endl;						
    
         //count dooflingy totals
         while(dooflingy > 0)		/*loop executes, but stops w/ break*/
         {
              if(dooflingy > 50)		//huge container is a quantity of 50
              {
                    huge++;
                    dooflingy -= 50;                       
                    break;
              }
             else if(dooflingy > 20)		//large container is a quantity of 20
             {
                   large++;
                   dooflingy -= 20;
                   break;
              }
             else if(dooflingy > 5)	             //medium container is a quantity of 5
             {
                   medium++;
                   dooflingy -= 5;
                   break;
              }
              else if(dooflingy > 1)		//small container is a quantity of 1
             {
                   small++;
                   dooflingy -= 1;
                   break;
              }
                else
                     break;
    	 }
    
    	 //display output
    	 cout << "\nContainer\tNumber"  
    	 << "\n=========\t======"  
    	 << "\n  Huge\t\t   "  << huge 
    	 << "\n  Large\t\t   "  << large
    	 << "\n  Medium\t   "  << medium
    	 << "\n  Small\t\t   "  << small << endl;
    
    	 return 0;
    }

  2. #2
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    try using continue. break will force execution to continue after the loop block (outside of the loop), and continue will make execution jump to the loop control statement.

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Can you post your attempt without any break statements.
    On the face of it, it would seem to be fine without them.
    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.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You don't need any of the breaks or continues, by the way. Using continue is kind a meaningless because the parts of the loop you want to skip are skipped anyway by the if else chain. Eventually the input number becomes 0 and the loop exits naturally.

    There's also another solution with division and modulus:
    Code:
        huge = dooflingy / 50;
        dooflingy &#37;= 50;
        large = dooflingy / 20;
        dooflingy %= 20;
        medium = dooflingy / 5;
        dooflingy %= 5;
        small = dooflingy / 1;
    If you have heard of arrays, you might be able to see a pattern here and turn that into a nice 3-line loop
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  3. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  4. char copy
    By variable in forum C Programming
    Replies: 8
    Last Post: 02-06-2005, 10:18 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM