BEGINNER discount
Nov 15, 2015 at 10:03pm UTC
So I dont know what's wrong with my code. I have to do this task for school:
If you are buying more than 5 things, butless than 20 - you get 5% off. If you are buying 10 and more things you get 10% off.
Write a program, which would count how much you need to pay for products. 'n' is for price and 'q' is for quantity.
It runs, but shows the wrong number
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int q;
float n;
cin>>q>>n;
if (q>5&&q<20){
n=n*q-((5*n)/100*q);
cout<<n;
} else if (q>=20){
n=n*q-((10*n)/100*q);
cout<<n;}
else
cout<<"no discount" ;
Last edited on Nov 16, 2015 at 5:20am UTC
Nov 15, 2015 at 10:06pm UTC
Your math is... well, not close.
What is the formula for total given a price, quantity and discount?
Nov 15, 2015 at 10:12pm UTC
By proportion 100%-n
5%-x
I find the value of discount for 1 product, by multiplying it by q i get a total value of discount of products
Nov 15, 2015 at 10:15pm UTC
And then you subtract it from the price of one product?
Nov 16, 2015 at 5:13am UTC
ok, but even with the linen=n*q-((5*n)/100*q);
it does not work
Nov 16, 2015 at 5:24am UTC
ok, but even with the linen=n*q-((5*n)/100*q);
it does not work
What makes you think it doesn't work?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// http://ideone.com/t5zqeP
include <iostream>
int main()
{
double n = 1.00;
int q = 6;
n = n*q - ((5 * n) / 100 * q);
std::cout << "Expected: 5.7\n" ;
std::cout << "Actual: " << n;
n = 2.50;
q = 100;
n = n*q - ((10 * n) / 100 * q);
std::cout << "Expected: 225\n" ;
std::cout << "Actual: " << n << '\n' ;
}
Expected: 5.7
Actual: 5.7
Expected: 225
Actual: 225
Nov 16, 2015 at 5:28am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#include <iostream>
int main()
{
int numberItems = 0;
std::cout << "# of items? " ;
std::cin >> numberItems;
float totalPrice = 0.0;
std::cout << "\nTotal price? " ;
std::cin >> totalPrice;
if (numberItems >= 10)
{
std::cout << "\n10% discount!\n" ;
totalPrice *= .9; // 10% discount, 90% of total price
}
else if (numberItems > 5)
{
std::cout << "\n5% discount!\n" ;
totalPrice *= .95; // 5% discount, 95% of total price
}
else
{
std::cout << "\nNo discount!\n" ;
}
std::cout << "You owe " << totalPrice << std::endl;
return 0;
}
Topic archived. No new replies allowed.