-
Help with Quanity discounts in C++
Ok im not sure how I would write a program for this one.
A software company sells a package that retails for $99. Quanity discounts are given according to the following table.
Quantity Discount
10-19 20%
20-49 30%
50-99 40%
100 or more 50%
Now, how would i write a program that will ask for the number of units sold and computes the total cost of the purchase?
I also have to make sure the number of units is greater than 0.
If anyone can help or at least get me started with writing a code for this
Thanks in advance!
-
Re: Help with Quanity discounts in C++
-
Re: Help with Quanity discounts in C++
well, the most useful construct in C++ for this purpuse is if (condition) { code exectued when condition is true } else { code exectued when condition is false }.
So for example if you want to apply a different rule from 1 to 9 to the interval 10 to 19, you would write:
Code:
if (amount >= 1 && amount <= 9) {
// do something
} else if (amount >= 10 && amount <= 19) {
// do something else
}
-
Re: Help with Quanity discoundts in C++
ok and you go down the table and put if statements for each quantity amounts?
-
Re: Help with Quanity discoundts in C++
-
Re: Help with Quanity discoundts in C++
also how do i calculate the discount and how does 99 dollars come into it?
-
Re: Help with Quanity discoundts in C++
Price tag = $99 per package.
If I buy 15 packages - I fall in the first discount category (20%) and hence Price for those 15 packages would not be 15 times $99 but 15*$99 - 15*$99*0.2 (0.2 comes from 20%)
Similary, you can get the prices for the rest of the discount categories given the count of packages one buys.
-
Re: Help with Quanity discoundts in C++
so far i have
#include<iostream>
using namespace std;
int main()
{
double quantity, discount, units
cout << "How many units were sold? ";
cin >> units;
if(quantity >= 10 && quantity <= 19)
//not sure what to put here?
if(quantity >= 20 && quantity <= 49)
//not sure what to put here?
if(quantity >= 50 && quantity <= 99)
//not sure what to put here?
if(quantity >= 100 && quantity <= 100)
//not sure what to put here?
so far does this look right?
-
Re: Help with Quanity discoundts in C++
Create two more variables - price and discounted price. Initialize price to 99 and discounted price to 0. Then inside each if you would do something like this:
Code:
if(quantity >= 10 && quantity <= 19)
{
discounted_price = <expression that does the calculation as I showed in my previous post>;
}
And in the end, put a cout to show the discounted price... Once you get this done.. and you still wish to improve the code - ideas are not scarce. ;)
-
Re: Help with Quanity discoundts in C++
like this?
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
double quantity, discount, units, price, discounted_price;
price = 99;
discounted_price = 0;
cout << "How many units were sold? ";
cin >> units;
if(quantity >= 10 && quantity <= 19)
discounted_price = <quantity*99 - quantity*99*0.2>;
if(quantity >= 20 && quantity <= 49)
discounted_price = <quantity*99 - quantity*99*0.3>;
if(quantity >= 50 && quantity <= 99)
discounted_price = <quantity*99 - quantity*99*0.4>;
if(quantity >= 100 && quantity <= 100)
discounted_price = <quantity*99 - quantity*99*0.5>;
cout << fixed << showpoint << setprecision(2);
cout << "The price with the discount is " << discounted_price << endl;
return 0;
-
Re: Help with Quanity discoundts in C++
Quote:
Originally Posted by joelw
if(quantity >= 10 && quantity <= 19)
discounted_price = <quantity*99 - quantity*99*0.2>;
if(quantity >= 20 && quantity <= 49)
discounted_price = <quantity*99 - quantity*99*0.3>;
if(quantity >= 50 && quantity <= 99)
discounted_price = <quantity*99 - quantity*99*0.4>;
if(quantity >= 100 && quantity <= 100)
discounted_price = <quantity*99 - quantity*99*0.5>;
No, no! The angled brackets in my post were just for illustration that should be replaced by actual valid expression. So, you would write like this:
Code:
if(quantity >= 10 && quantity <= 19)
{
discounted_price = quantity*99 - quantity*99*0.2;
}
or like this:
Code:
if(quantity >= 10 && quantity <= 19)
{
discounted_price = (1.0-0.2)*quantity*99;
}
Also, note that you do not consider the case when quatity is >0 but < 10. Also the last if statement for >=100 does not need a <= part. That makes the check for quantity==100 - but what you want is for anything that is >=100 gets 50% discounted. So it will just have if (quantity >=100). Hope this helps.
-
Re: Help with Quanity discoundts in C++
so other then that it looks good though?
-
Re: Help with Quanity discoundts in C++
Quote:
Originally Posted by joelw
so other then that it looks good though?
Yes, but did you try compiling the code and running it, if it works okay? That is what is called unit testing. Whenever you write a program, you test it to check if it is doing what it is supposed to do, that it meets the requirements. Do that to confirm if it is running fine.
-
Re: Help with Quanity discoundts in C++
ok i tried to compile this program and for the discount its giving me 0.00 everytime for everyone of the amount of units i use here is the code again must be something wrong in the math?
[c++]
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
double quantity,units, price, discounted_price;
price = 99;
discounted_price = 0;
cout << "How many units were sold? ";
cin >> units;
if(quantity >= 10 && quantity <= 19)
{
discounted_price = quantity*99 - quantity*99*0.2;
}
if(quantity >= 20 && quantity <= 49)
{
discounted_price = quantity*99 - quantity*99*0.3;
}
if(quantity >= 50 && quantity <= 99)
{
discounted_price = quantity*99 - quantity*99*0.4;
}
if(quantity >= 100)
{
discounted_price = <quantity*99 - quantity*99*0.5>;
}
cout << fixed << showpoint << setprecision(2);
cout << "The price with the discount is " << discounted_price << endl;
return 0;
}
[/c++]
if you could help me compiles and no errors at all.
-
Re: Help with Quanity discoundts in C++
1) Please use code tags when posting code.
Quote:
Originally Posted by joelw
Code:
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
double quantity,units, price, discounted_price;
price = 99;
discounted_price = 0;
cout << "How many units were sold? ";
cin >> units;
if(quantity >= 10 && quantity <= 19)
Look at that last line, and look at your code before the last line.
What is the value of quantity when you do that if() statement? That's right, it has no known value because you didn't give it one. You are using quantity, and you don't even have a value for it.
All you did was declare quantity -- just declaring it within a function does not establish a value for it. In other words, quantity is uninitialized, meaning it has a random garbage value which could be 0, 14.23, 2543.002, -23423432.9801789, anything.
Second,
Code:
quantity*99 - quantity*99*0.4;
when multiplying doubles by a constant (99), make your constants doubles also:
Code:
quantity*99.0 - quantity*99.0*0.4;
Regards,
Paul McKenzie
-
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
-
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.
Quote:
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?
Quote:
And Third, what do you mean when multiplying doubles by a constant (99), make your constants doubles also?
No.
Regards,
Paul McKenzie
-
Re: Help with Quanity discoundts in C++
so it should be the other way?
if(quantity <= 10 && quantity >= 19)?
-
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
-
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
-
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;
}
-
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.
-
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?
-
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?
-
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.
-
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
-
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.
-
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.
-
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.
-
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.
-
Re: Help with Quanity discoundts in C++
If I'm understanding the problem right, just post the code for one of the "normal" functions here and I'll point out how it would need to be changed to use pointers.
While GCDEF is right that unnecessary pointer use is a Bad Thing, exceptions may be made for class assignments since often the easiest way to introduce a concept is to show it doing the same thing you could do another way. I suspect that's the intent here.