While Loop Problem

can one of you guys maybe explain the following function to me?

the manager of a packaging business where all the containers have one size, namely 2000cm3. a C++ program that will determine how many containers will be needed for every order. For example: If the items are all of size 25cm3, the number that will fit into one container is 2000/25 = 80. If 200 items should be packed, 3 containers will be needed.

write a function, namely nrFitInContainer, of return type int. There is one value parameter only. The function receives an int value, namely the size of the individual items that should be packed. The function should then calculate how many of these items will fit into one container and return the answer as an integer.

Coding:

#include <iostream>
using namespace std;

const int CONTAINER_SIZE = 2000;

// The function nrFitInContainer should be inserted here if I'm correct?

int main( )
{
int size, nrInEveryContainer;
cout << "What is the size of every item? ";
cin >> size;
if (size > CONTAINER_SIZE)
cout << "Items are too big. Sorry, we cannot help you."
<< endl << endl;
else
{
nrInEveryContainer = nrFitInContainer(size);
cout << nrInEveryContainer
<< " items fit into one container." << endl;
}
return 0;
}
 
Aqox, as you said there is a big difference between the 2 and || fixed the loop problem.
 
Test 1: 5, 6, 7 = 18 sum = stays in loop
Test 2: 100, 100, 100 = 300 sum = stays in loop

*Test 3: 101, 101, 101 = 303 sum = quits program
*Test 4: 999, 10, 10 = 1019 sum = quits the program
*Test 5: 999, 10, 101 = 1110 = quits the program

Test 4 is incorrect.
 
--> the question says "If this condition does not hold", then you go back into the loop.

while !(sum < 1000) || n3 > 100)

The question also requires validation on the data being input by the user, you need to check if the data being entered is numeric.
 
IMPOSSIBLE!!!!!!!!!!!!! hehe - i tested it again, and it says what it does - quits the program... maybe your compiler is broken?

lol But it shouldn't quit in that case.

"The requirement [to quit] is that the sum of the three integers is less than 1000 or that n3 is greater than 100. "

Test 4
sum of 3 numbers = 1019 = FALSE
3rd number = 10 = FALSE
 
Aqox but in this case it should be OR??

I'm confused now as the program still doesn't work as it should.

//loop if sum is less than 1000 OR n3 is greater than 100
} while (!(sum < 1000)||(n3 > 100));

Test 1: 1,2,3 = 6 sum is less than a 1000 and N3 is smaller than 100. according to the coding this should loop and mine quite
 
lol But it shouldn't quit in that case.

"The requirement [to quit] is that the sum of the three integers is less than 1000 or that n3 is greater than 100. "

Test 4
sum of 3 numbers = 1019 = FALSE
3rd number = 10 = FALSE

ok I think I understand it. if the sum is less than 1000 quite or n3 is > than 100
 
Aqox but in this case it should be OR??

I'm confused now as the program still doesn't work as it should.

//loop if sum is less than 1000 OR n3 is greater than 100
} while (!(sum < 1000)||(n3 > 100));

Test 1: 1,2,3 = 6 sum is less than a 1000 and N3 is smaller than 100. according to the coding this should loop and mine quite

sum of 6 means quit, so it's correct. ;)
 
now that I understand that is there someone that can maybe look at the function question I had in the post?
 
In that other function, you obviously have to see how many whole items you can get into a package - C# has made me lazy, as you just assign the result of the division to an integer and it will round it down for you.

I can't remember what C++ has to offer. Is there an operator that can divide and then floor the decimal down to the nearest integer in a clean manner?
 
Add: #include <math.h>

Function:

int nrFitInContainer(double size) {
//return int((double)CONTAINER_SIZE/size);
return floor(CONTAINER_SIZE/size);
}

If you can't use math.h the commented out section will just cast it back to an int, getting rid of all the decimal places.
 
t123, if I use the "//return int((double)CONTAINER_SIZE/size);" it works fine. What is the reason for the (double) before CONTAINER? Because if I remove it the program also runs fine with out decimals
 
The (double) casts the return type to a double precision float. It works without it because you are probable assigning the return value (when you use the function) to an integer, which would implicitly cast the double to an int
 
that means if I want the return value as a interget I can leave out the double?
 
You can use this instead:
int nrFitInContainer(int size) {
return CONTAINER_SIZE/size;
}

Just be aware that the result is truncated because you're dividing to ints. In this case it's actually what you want to happend (the equivalent of calling floor), but may not always be so. If you wanted to round the result for exmaple you'd have to cast it to double or float, otherwise you'd get incorrect results.
 
Top
Sign up to the MyBroadband newsletter
X