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