If else then
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: If else then

  1. #1
    Join Date
    Feb 2009
    Posts
    18

    If else then

    It doesn't display the cost correctly. It just calculates the total x without taking into account the discount. What would I have to change/add to get the total cost after it calculates the discount?

    cout<<"Total cost " << cost;

    cost=(cost * 50);
    discount = (50 - cost);

    if (cost < 5)
    cost=(cost * 50);

    else if (cost > 5 && cost < 10);
    cost=(50 - (50 * 0.2));

    else if (cost > 10 && cost < 20)
    cost=(50 - (50 * 0.3));

    else if (cost > 20 && cost < 30)
    cost=(50 - (50 * 0.4));

    else if (cost > 30);
    cost=(50 -(50 * 0.5));
    Last edited by Guru_Kid; September 1st, 2009 at 12:32 AM.

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,297

    Re: If else then

    Quote Originally Posted by Guru_Kid View Post
    cout<<"Total cost " << cost;

    cost=(cost * 50);
    discount = (50 - cost);

    if (cost < 5)
    cost=(cost * 50);

    else if (cost > 5 && cost < 10);
    cost=(50 - (50 * 0.2));

    else if (cost > 10 && cost < 20)
    cost=(50 - (50 * 0.3));

    else if (cost > 20 && cost < 30)
    cost=(50 - (50 * 0.4));

    else if (cost > 30);
    cost=(50 -(50 * 0.5));
    Although I don't have time to fully understand your code, I may have found your problem. here:

    Code:
    else if (cost > 5 && cost < 10); //Adding this semi-colon makes the if do nothing
    cost=(50 - (50 * 0.2)); //this line always happens, it is not in the if
    My recommendation is to ALWAYS use brackets {} after an if/while/for, no exception. This will avoid you hours on end of headaches like this.

    Also, try to avoid code like this:
    Code:
    cost > 10 && cost < 20
    and prefer
    Code:
    (cost > 10) && (cost < 20)
    in this case, it is alright not to put the parenthesis, but one day you (or someone reading your code) will mess up the precedence. Also, it is clearer.

  3. #3
    Join Date
    Feb 2009
    Posts
    18

    Re: If else then

    Thanks. I edited some of the things you suggested. However, the discount part of it still isn't working. I updated my first post.

  4. #4
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: If else then

    You are calculating the total_cost before calculating the discount.
    and your if / else can be a lot sinpler

    Code:
    if (x < 10)
        discount=(0.0);    // less then 10 no discount
    else if (x < 20)       // not < 10 and < 20
        discount=(0.2);
    else if ( x < 40 )     // not < 20 and < 40
        discount=(0.3);
    else if (x < 50)
        discount=(0.4);
    else                   // 50 and more
        discount=(0.5);
    
    value=(cost*discount);
    total_cost= (cost - value);
    Kurt

  5. #5
    Join Date
    Feb 2009
    Posts
    18

    Re: If else then

    What would I have to change/add to get the total cost after it calculates the discount?

  6. #6
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,297

    Re: If else then

    Quote Originally Posted by ZuK View Post
    You are calculating the total_cost before calculating the discount.
    and your if / else can be a lot sinpler

    Code:
    if (x < 10)
        discount=(0.0);    // less then 10 no discount
    else if (x < 20)       // not < 10 and < 20
        discount=(0.2);
    else if ( x < 40 )     // not < 20 and < 40
        discount=(0.3);
    else if (x < 50)
        discount=(0.4);
    else                   // 50 and more
        discount=(0.5);
    
    value=(cost*discount);
    total_cost= (cost - value);
    Kurt
    Please submit a short but COMPLETE example of your code. It makes our lives easier, and we can help you better.

    My guess is that you declared "discount" as an integer (ie int/short/char), so it is always 0 (0.5 converts to 0 in integers).
    try using a floating point type (ie double, for example), and your problems should be fixed.

    I tried your example with:

    int: with x=20 then Result is 0
    float: with x=20 then Result is 35

  7. #7
    Join Date
    Feb 2009
    Posts
    18

    Re: If else then

    In my original post, I posted that it was declared as double.

    I'm not sure how you got 35. When I entered 20, I result is 500 (half * ( original x *50))

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,294

    Re: If else then

    What is your current code, keeping in mind ZuK's post #4? Remember to post your well indented code in [code][/code] bbcode tags.

    Quote Originally Posted by monarch_dodra
    Also, try to avoid code like this:
    On the other hand, superfluous parentheses make it more difficult to read code. The problem is determining just when they are superfluous and when they are beneficial despite being unnecessary, and that determination varies with the reader.
    Last edited by laserlight; August 31st, 2009 at 06:54 AM.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    Join Date
    Feb 2009
    Posts
    18

    Re: If else then

    Updated the first post
    Last edited by Guru_Kid; August 31st, 2009 at 08:10 AM.

  10. #10
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,294

    Re: If else then

    You have a bug here:
    Code:
    else
    if (x > 50);
    discount=(0.5);
    It should be:
    Code:
    else if (x > 50)
        discount = 0.5;
    or simply:
    Code:
    else
        discount = 0.5;
    Otherwise discount is always set to 0.5.

    Also, this looks strange:
    Code:
    if (x < 10)
        discount=(x * 50);
    Notice that in my code snippets I am indenting your code. You should indent your own code.

    EDIT:
    Another thing: your use of || is not just unnecessary, but incorrect. It would be more correct as &&, but then you would have errors on boundary conditions (e.g., if x == 10). Just test for x less than the given number.
    Last edited by laserlight; August 31st, 2009 at 07:08 AM.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  11. #11
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,107

    Re: If else then

    Think about your logic here.

    else if (x >30 || x <40)


    Let's say x is 10. That statement will evaluate true because x < 40.

    Let's say x is 110. That statement will still evaluate true because x > 30.

    So, aside from syntax errors, you'll need to get your logic straightened out to make it work. Please give it a little thought before just asking how to fix he logic.

  12. #12
    Join Date
    Feb 2009
    Posts
    18

    Re: If else then

    Quote Originally Posted by laserlight View Post
    You have a bug here:
    I changed what you suggested.

    My program still isn't coding correctly. It doesn't calculate the discount. Changed it a little.

    Code:
    double cost;
    double total_cost;
    double value;
    int x;
    float discount;
    
    cout<<"Enter the number of units sold: ";
    cin>> x;
    
    
    cost=(x * 50);
    
    if (x < 10)
    discount=(0);
    
    else if (x > 50);
    discount=(0.5);
    
    value=(cost*discount);
    total_cost= (cost - value);
    
    
    cout<<"Total cost " << total_cost;

  13. #13
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,294

    Re: If else then

    You still have the extra semi-colon.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  14. #14
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,107

    Re: If else then

    Quote Originally Posted by Guru_Kid View Post

    Code:
    cost=(x * 50);
    
    discount=(0);
    
    discount=(0.5);
    
    value=(cost*discount);
    total_cost= (cost - value);
    You don't need any of those parenthesis and I tole you a couple of posts ago why your discount calculation won't work.

  15. #15
    Join Date
    Feb 2009
    Posts
    18

    Re: If else then

    Solved. Thanks x)

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center