GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Jun-2007, 10:50
bridger31 bridger31 is offline
New Member
 
Join Date: May 2007
Posts: 12
bridger31 is on a distinguished road

Loop not executing, need a code review


I am having issues with this program executing, as far as I can see there aren't any issues with the structure - which is obviously wrong. There are no errors or warnings when compiled, so I am not sure where to turn. I can not get past the initial input stage, it seems that the while loop is not executing, therefore not printing my output. Hopefully a new pair of eyes on this will help, thanks.

CPP / C++ / C 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;						

     //count dooflingy totals
     while(dooflingy > 0)		/*loop will not execute*/
     {
          if(dooflingy > 50)		//huge container is a quantity of 50
          {
                huge++;
                dooflingy -= 50;
          }
         else if(dooflingy > 20)		//large container is a quantity of 20
         {
               large++;
               dooflingy -= 20;
          }
         else if(dooflingy > 5)	             //medium container is a quantity of 5
         {
               medium++;
               dooflingy -= 5;
          }
          else if(dooflingy > 1)		//small container is a quantity of 1
         {
               small++;
               dooflingy -= 1;
          }
	 }

	 //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  
Old 01-Jun-2007, 11:18
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,627
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Loop not executing, need a code review


Quote:
Originally Posted by bridger31
I can not get past the initial input stage

Make the program tell you exactly what it is seeing and what it is working with:

CPP / C++ / C Code:
.
.
.
    cin >> dooflingy;
    cout << "You entered " << dooflingy << endl;

    //count dooflingy totals
    while (dooflingy > 0) {     /*loop will not execute */
        cout << "Loop: dooflingy = " << dooflingy << endl;
.
.
.

(Stand by to hit ctrl-c.)

Regards,

Dave
  #3  
Old 01-Jun-2007, 15:05
bridger31 bridger31 is offline
New Member
 
Join Date: May 2007
Posts: 12
bridger31 is on a distinguished road

Re: Loop not executing, need a code review


I had 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.
CPP / C++ / C 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;
}
  #4  
Old 01-Jun-2007, 16:00
dlp dlp is offline
Junior Member
 
Join Date: May 2006
Posts: 88
dlp will become famous soon enough

Re: Loop not executing, need a code review


Here's a clue: What happens inside the loop when dooflingy = 1? Think about what each if statement checks for and you'll see what is happening.
  #5  
Old 01-Jun-2007, 19:10
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,627
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Loop not executing, need a code review


Quote:
Originally Posted by bridger31
I had a loop that was executing in infinity. Now my issue is that I have included break statements for each container size


Walk through it "by hand". What if you had entered, say, 100? How many times would you go through the loop? You certainly don't want to break out of the loop whenever you find something, right?

Now, what if you had entered, say, 40? How many times would you go through the loop?

Your structure in the original post was OK. The errors in the details (the comparisons) made it an infinite loop. Fix the comparisons, and all will be OK.

Regards,

Dave
  #6  
Old 01-Jun-2007, 20:17
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 405
Peter_APIIT is on a distinguished road
Thumbs up

Re: Loop not executing, need a code review


I have found out a problm but i don't know whether it is true or not.

You if statement has some problem. You should changed to a certain range such as
CPP / C++ / C Code:
  
if ( x>1 || < 5)


I hope this might help you.
__________________
Linux is the best OS in the world.
  #7  
Old 01-Jun-2007, 20:48
bridger31 bridger31 is offline
New Member
 
Join Date: May 2007
Posts: 12
bridger31 is on a distinguished road

Re: Loop not executing, need a code review


Thanks Dave, it was right there in front of me. Always seems like the problem should be harder, but it is the little mistakes that add up.
 
 

Recent GIDBlogLast Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Text-Based Roulette Game mfm1983 CPP / C++ Forum 5 29-Nov-2006 12:20
How to sort random access file? wmmccoy0910 C Programming Language 12 04-Sep-2006 03:40
Here it is again! 35% - 40% off For Life! my-e-space Web Hosting Advertisements & Offers 0 20-Apr-2006 14:48
Problem with int mixed with char,... leitz CPP / C++ Forum 17 07-Dec-2004 20:56
Segmentation error on executing socket code nkhambal C Programming Language 3 30-Aug-2004 12:53

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:57.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.