|
-
May 8th, 2009, 01:55 AM
#1
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();
}
-
May 8th, 2009, 02:10 AM
#2
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
-
May 8th, 2009, 07:19 AM
#3
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.
-
May 8th, 2009, 10:18 AM
#4
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.
-
May 8th, 2009, 11:02 AM
#5
Re: calculating discount percents
 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?
-
May 8th, 2009, 11:21 AM
#6
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.
-
May 8th, 2009, 11:24 AM
#7
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.
-
May 8th, 2009, 11:30 AM
#8
Re: calculating discount percents
 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.
-
May 8th, 2009, 11:35 AM
#9
Re: calculating discount percents
code is at the top of the post.
-
May 8th, 2009, 12:03 PM
#10
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.
Last edited by Amleto; May 8th, 2009 at 12:07 PM.
-
May 8th, 2009, 12:22 PM
#11
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);
}
-
May 8th, 2009, 12:32 PM
#12
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.
-
May 8th, 2009, 12:41 PM
#13
Re: calculating discount percents
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.
-
May 8th, 2009, 01:07 PM
#14
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.
-
May 8th, 2009, 01:41 PM
#15
Re: calculating discount percents
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|