CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 31
  1. #16
    Join Date
    Sep 2006
    Posts
    60

    Re: Help with Quanity discoundts in C++

    First off how do you use tages

    Second last night i was getting a warning that quantity wasnt intialized yet so i put quantity = 0 and still came up with 0.00 for the discount. Now, should quantity be 0?

    And Third, what do you mean when multiplying doubles by a constant (99), make your constants doubles also?

    Thanks in advance

  2. #17
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Help with Quanity discoundts in C++

    Quote Originally Posted by joelw
    First off how do you use tages
    Look at the # icon on the message window.
    Second last night i was getting a warning that quantity wasnt intialized yet so i put quantity = 0 and still came up with 0.00 for the discount. Now, should quantity be 0?
    If quantity is 0, what does the code do? Did you step through the code to see what happens if quantity is 0? The answer is obvious if you take a look at your if() statements.
    Code:
     if(quantity >= 10 && quantity <= 19)
    No.
    Code:
    if(quantity >= 20 && quantity <= 49)
    No.
    Code:
    if(quantity >= 50 && quantity <= 99)
    No.
    Code:
    if(quantity >= 100)
    No.

    So what will happen, now that none of those if() statements are true if quantity is 0? It is also confusing that you have variables called "units" and "quantity". What do each of these supposed to denote?
    And Third, what do you mean when multiplying doubles by a constant (99), make your constants doubles also?
    No.

    Regards,

    Paul McKenzie

  3. #18
    Join Date
    Sep 2006
    Posts
    60

    Re: Help with Quanity discoundts in C++

    so it should be the other way?

    if(quantity <= 10 && quantity >= 19)?

  4. #19
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Help with Quanity discoundts in C++

    Quote Originally Posted by joelw
    so it should be the other way?

    if(quantity <= 10 && quantity >= 19)?
    Just read that statement out loud and see if it makes sense.

    "If quantity is 10 or less, and at the same time 19 or more..."
    Will this condition ever be true?

    - petter

  5. #20
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Help with Quanity discoundts in C++

    As Paul McKenzie mentioned, why do you use both 'units' and 'quantity'? The user input is placed in 'units', but your if-statements etc. are using 'quantity'.

    - petter

  6. #21
    Join Date
    Sep 2005
    Location
    United States
    Posts
    799

    Re: Help with Quanity discoundts in C++

    It is pretty obvious that you are just learning C++, because you are having trouble with some of the most basic concepts of the language. That's ok, but it's honestly in your best interest to solve problems on your own before coming to forums like this to get people to solve your homework problems for you. We are here to help you with problems but that is assuming that you have already written all of your code. Perhaps you are getting some crazy debugging error or runtime issue, things like that.

    With that being said, you did make a good attempt at writing your own code once you got a little "nudge" from some of the other members. I think the main problem with the code you wrote is this, as Paul McKenzie pointed out: you didn't cover all of the possible cases for the number of units sold. You didn't account for what will happen if the user enters 0 or a negative number in your program. It probably won't crash, but you will get undefined, and unwanted results. You also had a few syntax errors and some other minor things typical of a person just learning C++, like not indenting within different levels of code. Other than that though, most of it was correct.

    I strongly suggest that you do as many more examples of coding as you can, and pay greater attention in class at school. If you are having trouble with if-else statements, you will be in BIG trouble later when you start working with pointers, classes, and polymorphism Good luck.


    Parts that are bold are areas I felt you had problems in....
    Code:
    #include<iostream>
    #include<iomanip>
    
    using namespace std;
    
    int main()
    {
    
    //Initialize variables here
    int quantity = 0;
    double price = 99.0;
    double discounted_price = 0.0;
    
    //Collect input from user
    //Should only be ONE variable for input here
    //No need for units AND quantity if you only use one of them
    cout << "How many units were sold? ";
    cin >> quantity;
    
    //Illegal; Negative numbers and zero are not allowed
    if ( quantity <= 0 )
    {
       cout << "At least one unit must be purchased!" << endl;
       exit(1);
    }
    
    //No discount here
    if ( quantity >= 1 && quantity <= 10 )
    {
       discounted_price = quantity*99;
    }
    
    //20% discount here
    else if( quantity >= 11 && quantity <= 19 )
    {
       discounted_price = quantity*99 - quantity*99*0.2; 
    }
    
    //30% discount here
    else if( quantity >= 20 && quantity <= 49 )
    {
       discounted_price = quantity*99 - quantity*99*0.3; 
    }
    
    //40% discount here
    else if( quantity >= 50 && quantity <= 99 )
    {
       discounted_price = quantity*99 - quantity*99*0.4;
    }
    
    //50% discount here
    else if( quantity >= 100 )
    {
       discounted_price = quantity*99 - quantity*99*0.5;
    }
    
    //Display results
    cout << fixed << showpoint << setprecision(2);
    cout << "The price with the discount is " << discounted_price << endl;
    
    return 0;
    
    }
    Last edited by dcjr84; September 29th, 2006 at 02:27 PM.
    Please rate my post if you felt it was helpful

  7. #22
    Join Date
    Oct 2008
    Posts
    3

    Re: Help with Quanity discoundts in C++

    Hey guys, I am doing the same exact problem in my 210 class. The difference is that we have to write the program using pointers. My only question is what is the least number of functions i need to declare?
    I declared a function for each quantity level
    one for 10-19
    one for 20-49
    one for 50-99
    one for 99-up
    and also a function to calc discount.

    I am also a noob and I just learned pointers so I am having trouble at finding the most efficient way to declare my functions.

  8. #23
    Join Date
    Oct 2008
    Location
    Tel Aviv, Berlin, L.A.
    Posts
    23

    Re: Help with Quanity discoundts in C++

    question(s) for you:

    a) what to u pay exterminator for writing your code?
    b) how about sticking with JavaScript, or so, instead of C?
    ariell
    programming is understanding

  9. #24
    Join Date
    Oct 2008
    Location
    Tel Aviv, Berlin, L.A.
    Posts
    23

    Re: Help with Quanity discoundts in C++

    question(s) for you:

    a) what do u pay exterminator for writing your code?
    b) how about sticking with JavaScript, or so, instead of C?
    ariell
    programming is understanding

  10. #25
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Help with Quanity discoundts in C++

    Quote Originally Posted by Danpiz
    Hey guys, I am doing the same exact problem in my 210 class. The difference is that we have to write the program using pointers. My only question is what is the least number of functions i need to declare?
    I declared a function for each quantity level
    one for 10-19
    one for 20-49
    one for 50-99
    one for 99-up
    and also a function to calc discount.

    I am also a noob and I just learned pointers so I am having trouble at finding the most efficient way to declare my functions.
    I don't see how pointers have anything to do with the task at hand. You use pointers when you need them, not just for fun.

    The logic seems to be pretty well explained already. You don't need a different calculation for each quantity.

    I have no idea what ariell is trying to say.

  11. #26
    Join Date
    Oct 2008
    Posts
    3

    Re: Help with Quanity discoundts in C++

    My teacher says that we have to write all our programs using functions and pointers. The code for this problem is provided in our book, but since we are learning pointers, my teacher told us to rewrite the code to our "fashion". I was just wondering what functions I need to declare. There were a couple other students in class that ive seen there code and they all declared a diff # of pointers. So I was wondering whats the easiest way to do it. thanks

  12. #27
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Help with Quanity discoundts in C++

    My response doesn't change. From the first post, I don't see the need for pointers here. "Rewrite the code using pointers" makes no sense.

    As to how many functions you need, you start by identifying the logical steps the program has to perform, then dividing them up into functions.

  13. #28
    Join Date
    Oct 2008
    Posts
    3

    Re: Help with Quanity discoundts in C++

    if your reponse dosent change from the first post, then dont even bother posting. Sorry, but im not going to tell my teacher his instructions dont make sense. You obviously have bad social behavior beacuse all your post sound like your trying to be a smartass. If you dont like my post or my questions dont respond. There are plenty of C++ forums I have posted on and the so called "Elite" members dont act like you.

  14. #29
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Help with Quanity discoundts in C++

    Quote Originally Posted by Danpiz
    Sorry, but im not going to tell my teacher his instructions dont make sense.
    Most teachers are perfectly happy to offer clarification when asked, FYI.

  15. #30
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Help with Quanity discoundts in C++

    Quote Originally Posted by Danpiz
    if your reponse dosent change from the first post, then dont even bother posting. Sorry, but im not going to tell my teacher his instructions dont make sense. You obviously have bad social behavior beacuse all your post sound like your trying to be a smartass. If you dont like my post or my questions dont respond. There are plenty of C++ forums I have posted on and the so called "Elite" members dont act like you.
    Wow. There's a rash of young kids that think they know it all around here lately. Good luck in the real world.

    Your question didn't change, why should my response? What do you want us to do when you ask a nonsensical question and then repeat it? You can ask it 30 times, it still won't make sense, and whether you like it or not, my answer is the only possible on you can get at this point.

Page 2 of 3 FirstFirst 123 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
  •  





Click Here to Expand Forum to Full Width

Featured