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;
}