if statment question

This is a discussion on if statment question within the C++ Programming forums, part of the General Programming Boards category; item 1 and item 2 have the same cost and a 10 percent discount if the quantity is over 15 ...

  1. #1
    Carsten
    Guest

    Unhappy if statment question

    item 1 and item 2 have the same cost and a 10 percent discount if the quantity is over 15 units.
    All of my units are getting discounts if they are over 15 units and I am at a loose on this one here is my statement;

    Code:
    void Compute_Purchase_Price (void)
    {
    if ((item == 1) || (item == 2))
    {		
    		sub_tot = ITEM_12 * quantity;
    }
    if ((item == 3) || (item == 4))
    {
    		sub_tot = ITEM_34 * quantity;
    }
    if (((item == 1) || (item == 3)) && (quantity > 15))
    {
    		dis = DISCOUNT * sub_tot;
    }
    }
    any help is welcome.

    code tags added by Kermi3

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    Code Tags

    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happier about helping you

    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found on the code tag post at the top of every forum. I also suggest you take a look at the board guildlines if you have not done so already.

    This is a common first post mistake, just remember to use [code] tags in the future and you'll get much more help.

    If this is your first time posting here the welcome, and if there's anything I can do or any questions I can answer about these forums, or anything else, please feel free and welcome to PM me.


    Good Luck with your program,

    Kermi3
    Lead Moderator
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    2
    I think the problem might be in the last if statement. if you only want the discount to apply to items 1 and 2 then you are using the wrong conditional statement you should check if it is item 1 or item 2 not if it is item 1 or item 3. hope this helps.
    JG

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    from the looks of your code and what you said about items 1 and 2 having the same cost, you could do it this way...
    Code:
    void Compute_Purchase_Price (void)
    {
    	if ((item == 1) || (item == 2))
    	{
    		sub_tot = ITEM_12 * quantity;
                             if(quantity > 15)
    		{
    			dis = DISCOUNT * sub_tot;
    		}
    	}
    	if ((item == 3) || (item == 4))
    	{
    		sub_tot = ITEM_34 * quantity;
    	}
    }
    this way, it eliminates the unnecessary check of item 1 and 2 again...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 06:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 01:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 01:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21