If Else dealing with percentage

help with c++ problem. Thank You!!!!!!!!

a software company sells a package that retails for $99 . Quantity discounts are given according to this table.
Quantity Discount
10-19 20%
20-49 30%
50-99 40%
100 or more 50%

Write a program that asks for the number of units sold and computes the total cost of the purchase.

Input Validation: Make sure the number of units is greater than 0.

Here is what I wrote out, please tell me whats wrong with it.

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
int quantity, package = 99, cost;

cout << "How many units sold? ";
cin >> quantity;

if ( quantity >= 10 && quantity <= 19)
{
cost = package * quantity * 0.2;
cout << "Total cost is: " << cost << endl;
}
else if ( quantity >= 20 && quantity <= 49)
{
cost = package * quantity * 0.3;
cout << "Total cost is: " << cost << endl;
}
else if ( quantity >= 50 && quantity <= 99)
{
cost = package * quantity * 0.4;
cout << "Total cost is: " << cost << endl;
}
else if ( quantity > 100 )
{
cost = package * quantity * 0.5;
cout << "Total cost is: " << cost << endl;
}
else cost = package * quantity;

cout << "Total Cost is " << cost << endl;
system ("pause");
return 0;
}
Hey there, read the comments I included, they were the first thing I noticed wrong, i'm not quite sure how to validate that x is greater than 0.

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
34
35
36
37
38
39
40
41
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
double quantity, package = 99, cost; // declare variables as double since you are multiplying by .2

cout << "How many units sold? ";
cin >> quantity;

if ( quantity >= 10 && quantity <= 19)
{ 
cost = package * quantity - (0.2 * package * quantity); // your original equation was read like so. pa.ckage times quantity, now display 20% of that value. your goal is to discount 20%, not to charge them only 20%
cout << "Total cost is: " << cost << endl; 
}
else if ( quantity >= 20 && quantity <= 49)
{
cost = package * quantity - (0.3 * package * quantity);
cout << "Total cost is: " << cost << endl;
}
else if ( quantity >= 50 && quantity <= 99)
{
cost = package * quantity - (0.4 * package * quantity);
cout << "Total cost is: " << cost << endl;
}
else if ( quantity > 100 )
{
cost = package * quantity - (0.5 * package * quantity);
cout << "Total cost is: " << cost << endl;
}
else if (quantity <10 && quantity >=1)
{
cost = package * quantity;
cout << "Total cost is: " << cost << endl;
}

system ("pause");
return 0;
}


Again, i havent worked with else or if or else if statements yet, (still learning the more simple stuff) but reading your code is what quite apparent there were math errors.
Last edited on
Why don't you tell us what's wrong with it? Why do we have to guess?

Do you get compiler errors?

Does it run?

Does it not behave how you expect?

What do you expect? How is it behaving?

etc
etc

Tell us what the problem is so we can help you solve it. Pasting a glob of code and saying "help me" is a poor approach.


EDIT: but I see you got a reply anyway. Still, in the future give us more information when posting a question.
Last edited on
Thank you georgewashere (6) . It did not calculate the percentage, but I'll use your comments and changes to help make it work. I greatly appreciate your input.
georgewashere (6) It worked. I was entering 1 to test when I should have entered 10 or more. I have three more problems to do. I will use your code above to assist me.

When you don't know is a hell of a thing. The forum input goes a long way, you guys are the best, I will certainly help solve problems in the future.

Looking at the posted code, fixing it and reposting it gives us novice something to compare our ideas to, so that we can understand what we did wrong and grow from it. The comments in the code helped.


Again, thank You.
Topic archived. No new replies allowed.