calculating discount percents
Greetings.
New member and first time poster here.
I think i've got all the information needed, now i need help with the mathematics. Mainly, Showing the Subtotal for the item price after the discount is applied. I have googled and googled, but haven't found a clear concise way of doing it, so i came here.
I'm not sure if i was even suppose to paste the full code, so i apologize if i broke a rule.
I do have some more if statements to add, but i was wanting to get the basic layout completed first.
Also, as you can see below, there is two yes/no options. I think they need to be set as char variables but i am not sure.
anyways, take a look, if I've over complicated it, please let me know, if not, then great.
I always appreciate help.
!-------------------------Begin Code Here-----------------------------------!
Thanks for looking.
#include <iostream>
using namespace std;
int main()
{
int ItemPrice, ProductDiscountRate,SeniorDiscountRate,CustomerAge, DeliveryService, ZipCode;
cout << "Item Price:";
cin >> ItemPrice;
cout << "62 years or older? (Yes/No)";
cin >> CustomerAge;
cout << "Delivery? (Yes/No)";
cin >> DeliveryService;
cout << "Zip Code:";
cin >> ZipCode;
if ( ItemPrice >= 1500.00 ) { ProductDiscountRate = 25; }
if ( CustomerAge = 1 ) { SeniorDiscountRate = 10; }
if ( DeliveryService = 1) { DeliveryService = 20; }
cout << "Product Dicount:" << ProductDiscountRate << endl;
cout << "Senior Discount:" << SeniorDiscountRate << endl;
cout << "Delivery Service:" << DeliveryService << endl;
cout << "Zip Code:" << ZipCode << endl;
cin.get();
}
Re: calculating discount percents
Hi,
Watch out for this type of thing:
Code:
if ( CustomerAge = 1 ) { SeniorDiscountRate = 10; }
if ( DeliveryService = 1) { DeliveryService = 20; }
It should be:
Code:
if ( 1 == CustomerAge ) { SeniorDiscountRate = 10; }
if ( 1 == DeliveryService) { DeliveryService = 20; }
If you just use a single '=' you assign 1 to the variable, you're not comparing it with 1. You normally won't get any warning from the compiler, so it's a an easy mistake to make. I've not checked to see if there are any other problems.
Alan
Re: calculating discount percents
I'd break it onto separate lines to make it easier to read and debug too.
Code:
if ( 1 == CustomerAge )
{
SeniorDiscountRate = 10;
}
Not sure what the OP question is.
You probably do want char variables for the Yes/No questions. You're also going to need floating point data types to handle non-integer numbers.
Re: calculating discount percents
I'm trying to do discounts based on the product price and the customer age.
So if the product price is >=1500 then the discount would be ProductPrice * .25 and then if the customer is >=62 there's an addition 10% taken off the Productprice after the 25% has been taken off.
Re: calculating discount percents
Quote:
Originally Posted by
rottenstock
I'm trying to do discounts based on the product price and the customer age.
So if the product price is >=1500 then the discount would be ProductPrice * .25 and then if the customer is >=62 there's an addition 10% taken off the Productprice after the 25% has been taken off.
So, what's the problem?
Re: calculating discount percents
everytime i do it how i think it should be done, the result is 0.
I'm very new at this, but from the research i have done, it seems like i should use a float command, but i am still confused by that.
Basically, it's going to be:
ItemPrice * ProductDiscountRate (which should be a %, like .25) = Subtoal
and then the Senior Discount is suppose to be Subtotal * SeniorDiscountRate
But both of those ways return a value of 0.
Re: calculating discount percents
If your result is < 1 and you store it in an int, you'll get 0.
Without seeing the actual formula you're using, can't guess beyond that.
Re: calculating discount percents
Quote:
Originally Posted by
rottenstock
everytime i do it how i think it should be done, the result is 0.
I'm very new at this, but from the research i have done, it seems like i should use a float command, but i am still confused by that.
Basically, it's going to be:
ItemPrice * ProductDiscountRate (which should be a %, like .25) = Subtoal
and then the Senior Discount is suppose to be Subtotal * SeniorDiscountRate
But both of those ways return a value of 0.
Show your code. Ms. Cleo doesn't post here any more. Remember, as I said earlier, you'll need to use floating point data types.
Re: calculating discount percents
code is at the top of the post.
Re: calculating discount percents
that code doesnt contain any calculation that you say you use to get 0 for the final cost - show your code.
Re: calculating discount percents
whoops, sorry..
here's a repaste of what i've done, or attempted to do:
!-----------Code--------------!
#include <iostream>
using namespace std;
int main()
{
int ItemPrice, ProductDiscountRate,SeniorDiscountRate,CustomerAge, DeliveryFee,Subtotal,SeniorDiscountAmount;
char DeliveryService, ZipCode;
cout << "Item Price: ";
cin >> ItemPrice;
cout << "Customer Age: ";
cin >> CustomerAge;
if (ItemPrice >= 1500) {
ProductDiscountRate = 25;
}
else if (ItemPrice >= 750) {
ProductDiscountRate = 12;
}
else if (ItemPrice >= 375) {
ProductDiscountRate = 6;
}
else if (ItemPrice >= 188) {
ProductDiscountRate = 3;
}
if (CustomerAge <= 61) {
SeniorDiscountRate = 0;
}
else { SeniorDiscountRate = 10; }
cout << "Item Price: " << ItemPrice << endl;
cout << "Product Dicount: " << ProductDiscountRate << endl;
cout << "Senior Discount: " << SeniorDiscountRate << endl;
cout << "Subtotal: " << ItemPrice - (temPrice * ProductDiscountRate) << endl;
cin.get();
return (0);
}
Re: calculating discount percents
If ItemPrice is 2000, then ProductDiscountRate will be 25, so "Subtotal" will output
2000 - (2000 * 25) == -24*2000 = -48000.
Definitely not what you intended. I think you probably meant to divide ProductDiscountRate by 100.0.
Re: calculating discount percents
Quote:
Definitely not what you intended. I think you probably meant to divide ProductDiscountRate by 100.0.
You sire are correct. Tried changing it and it still doesnt work. Like i said, im new, and still confused by the float aspect.
Re: calculating discount percents
Did you divide by 100, or 100.0? The two are not the same to the machine.
In any case the next step is to set a breakpoint on the line in question, then jump in with the debugger and see what the values actually are in those variables when you get there.
Re: calculating discount percents
Re: calculating discount percents
Try typing these lines into the beginning of your program and see what happens:
Code:
for (int i = 0; i < 10; i++)
cout << i/4 << endl;
for (int i = 0; i < 10; i++)
cout << i/4.0 << endl;
Re: calculating discount percents
rottenstock, have you noticed that the layout when you post code differs from when others post code? That's because posts in this forum uses some formatting tags. You can read more about them here http://www.codeguru.com/forum/misc.php?do=bbcode
Re: calculating discount percents
everytime i do it how i think it should be done, the result is 0.
I'm very new at this, but from the research i have done, it seems like i should use a float command, but i am still confused by that.
Basically, it's going to be:
ItemPrice * ProductDiscountRate (which should be a %, like .25) = Subtoal
and then the Senior Discount is suppose to be Subtotal * SeniorDiscountRate
But both of those ways return a value of 0.
Government ********
Re: calculating discount percents
Quote:
Originally Posted by
christinejones
everytime i do it how i think it should be done, the result is 0.
I'm very new at this, but from the research i have done, it seems like i should use a float command, but i am still confused by that.
Basically, it's going to be:
ItemPrice * ProductDiscountRate (which should be a %, like .25) = Subtoal
and then the Senior Discount is suppose to be Subtotal * SeniorDiscountRate
But both of those ways return a value of 0.
********** ********
You're allowed to start your own thread. :)
Let's see your code.