Thread: Help Please

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    13

    Question Help Please

    Need a little help with this program please. How come it doesn't return any values (other than 0) for cost, tax, and charges?

    Here's the pseudocode that i coded from:
    Code:
    input qty
    while qty > 0
    	input size
    
    	call Calculate Scrap using 10 to get scrap 10, qty needed for 10
    	call Calculate Scrap using 25 to get scrap 25, qty needed for 25
    	call Calculate Scrap using 40 to get scrap 40, qty needed for 40
    	
    	if scrap 10 < scrap 25 and < scrap 40
    		set qty needed to qty needed for 10
    		set price to 2.10
    	else if scrap 25 < scrap 10 and < scrap 40
    		set qty needed to qty needed for 25
    		set price to 4.90
    	else
    		set qty needed to qty needed for 40
    		set price to 7.51
    
    	multiply qty needed by price to get cost
    	multiply cost by 1.75
    	multiply cost by .08 to get tax
    	add tax to cost to get charges
    	output cost, tax, charges
    	input qty
    
    
    Calculate Scrap
    	divide stock by size to get per stock
    	call floor using per stock
    	if size *(per stock + 1/16) > stock
    		subtract 1 from per stock
    	divide qty by per stock to get qty needed
    	call ceiling using qty needed
    	multiply per stock by size to get shipped
    	subtract shipped from stock to get scrap
    	multiply scrap by qty needed
    and the code I wrote:
    Code:
    #include<iostream>
    #include<math.h>
    using namespace std;
    
    int Calculate_Scrap(int qtyneeded, int size, int qty);
    
    int main()
    {
    	int qty, size, qtyneeded;
    	double price, cost, tax, charges;
    	int scrap10, scrap25, scrap40;
    
    	cout <<"\n Enter the quantity: " << endl;
    	cin >> qty;
    
    	while(qty > 0)
    	{
    		cout << "\n Enter the size: " << endl;
    		cin >> size;
    
    		scrap10 = Calculate_Scrap(qtyneeded = 10, size, qty);
    		scrap25 = Calculate_Scrap(qtyneeded = 25, size, qty);
    		scrap40 = Calculate_Scrap(qtyneeded = 40, size, qty);
    
    		if(scrap10 < scrap25 < scrap40)
    		{
    			qtyneeded = scrap10;
    			price = 2.10;
    		}
    		else if(scrap25 < scrap10 < scrap40)
    		{
    			qtyneeded = scrap25;
    			price = 4.90;
    		}
    		else
    		{
    			qtyneeded = scrap40;
    			price = 7.51;
    		}
    
    		cost = qtyneeded * price;
    		cost = cost * 1.75;
    		tax = cost * 0.08;
    		charges = tax + cost;
    
    		cout << "\n The cost is " << cost;
    		cout << "\n The tax is " << tax;
    		cout << "\n The charges are " << charges << endl;
    
    		cout << "\n Enter the quantity or 0 to exit: " << endl;
    		cin >> qty;
    	}			
    				
    	return 0;
    }
    
    
    int Calculate_Scrap(int qtyneeded, int size, int qty)
    {
    	double perstock, stock, shipped, scrap;
    	
    	stock = 10000;
    
    	perstock = stock / size;
    	floor(perstock);
    	if((size *(perstock + 1/16)) > stock)
    	{
    		--perstock;
    	}
    	qtyneeded = qty / perstock;
    	ceil(qtyneeded);
    	shipped = perstock * size;
    	scrap = stock - shipped;
    	return scrap * qtyneeded;
    	
    }
    I think I may be passing the values wrong to the function or returning the improper value. Let me know what you think. Any help is appreciated.

    Thanks!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Code:
    if(scrap10 < scrap25 < scrap40)
    You can't do that. Should be
    Code:
    if (scrap10 < scrap25 && scrap25 < scrap40)
    Same goes for the other similar lines.

    Edit: To be more precise, as CornedBee points out below, you CAN do that (which is why the compiler doesn't complain), but it doesn't mean the same thing in C++ that it does as a mathematical formula.
    Last edited by robatino; 12-08-2006 at 05:12 PM.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    > scrap10 = Calculate_Scrap(qtyneeded = 10, size, qty);
    While this happens to work, you should review function call syntax.

    > if(scrap10 < scrap25 < scrap40)
    This, however, most definitely won't work. The expression is parsed like this:
    (scrap10 < scrap25) < scrap40
    In words: Take the first expression. If scrap10 is less than scrap25, the expression has the value true, otherwise false. Compare this boolean value to the value of scrap40. Integral promotion means that false becomes 0 and true becomes 1.
    In total, the if is entered if scrap40 is greater than 0 or 1, depending on whether scrap10 is less than scrap25.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if((size *(perstock + 1/16)) > stock)
    Be careful when things get promoted to other types. The 1/16 is done using integer math (result will be 0), then that is implictly converted to 0.0 to be added to perstock (which is a double).

    > ceil(qtyneeded);
    > floor(perstock);
    These functions return results, like
    perstock = floor(perstock);
    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.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Forgot to mention, use header <cmath> instead of <math.h>.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    ok I made a few changes using your suggestions but I'm still running into a problem. The program doesn't always return values for cost, tax and charges. For instance if I enter 5 for quantity and 8 for size it will return 0's. It will return values for other numbers but sometimes they don't make sense.

    ie enter 4 for quantity, 6 for size - returns total charges of 9.21.
    enter 4 for quantity, 7 for size - returns total charges of 70.96.
    enter 4 for quantity, 9 for size - returns total charges of 56.77.
    What's going on here?

    Am I sending or returning the improper variables to the function?
    Also take a look at the pseudocode from my first post to make sure I have everything functioning the way it's supposed to.

    In need of your help.
    Thanks!

    Code:
    #include<iostream>
    #include<cmath>
    using namespace std;
    
    double Calculate_Scrap(double, double, double);
    
    int main()
    {
    	double qty, size, qtyneeded, stock;
    	double price, cost, tax, charges;
    	double scrap10, scrap25, scrap40;
    
    	cout <<"\n Enter the quantity: " << endl;
    	cin >> qty;
    
    	while(qty > 0)
    	{
    		cout << "\n Enter the size: " << endl;
    		cin >> size;
    
    		stock = 10;
    		scrap10 = Calculate_Scrap(stock, size, qty);
    		stock=25;
    		scrap25 = Calculate_Scrap(stock, size, qty);
    		stock=40;
    		scrap40 = Calculate_Scrap(stock, size, qty);
    
    		if(scrap10 < scrap25 && scrap10 < scrap40)
    		{
    			qtyneeded = scrap10;
    			price = 2.10;
    		}
    		else if(scrap25 < scrap10 && scrap25 < scrap40)
    		{
    			qtyneeded = scrap25;
    			price = 4.90;
    		}
    		else
    		{
    			qtyneeded = scrap40;
    			price = 7.51;
    		}
    
    		cost = qtyneeded * price;
    		cost = cost * 1.75;
    		tax = cost * 0.08;
    		charges = tax + cost;
    
    		cout << "\n The cost is " << cost;
    		cout << "\n The tax is " << tax;
    		cout << "\n The charges are " << charges << endl;
    
    		cout << "\n Enter the quantity or 0 to exit: " << endl;
    		cin >> qty;
    	}			
    				
    	return 0;
    }
    
    
    double Calculate_Scrap(double stock, double size, double qty)
    {
    	double perstock, shipped, scrap, qtyneeded;
    	
    	perstock = stock / size;
    	perstock = floor(perstock);
    	if((size *(perstock + 1/16)) > stock)
    	{
    		perstock = perstock - 1;
    	}
    	qtyneeded = qty / perstock;
    	qtyneeded = ceil(qtyneeded);
    	shipped = perstock * size;
    	scrap = stock - shipped;
    	return scrap * qtyneeded;
    	
    }

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I still see 1/16 being done in integer math (the result always being zero)
    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.

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    Quote Originally Posted by Salem
    I still see 1/16 being done in integer math (the result always being zero)

    How would I fix this?

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    If you put a decimal point after 1 and/or 16, that makes the constant double instead of integer, so the arithmetic is done in double precision. You could also just write .0625.

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    I got it working correctly now. Thanks everyone for you help. These forums are such a great learning resource and I can't thank everyone who contributes enough. Thanks for all your input!

Popular pages Recent additions subscribe to a feed