t-shirt discount

closed account (D4NbpfjN)
I need some help with this code. I have inputted the code that i have so far.

The local t.shirt shop sells shirts that retail for $12. Quantity discounts are given as follow:
Number of Shirts Discount
5–10 10%
11–20 15%
21–30 20%
31 or more 25%
Write a program that prompts the user for the number of shirts required and then computes
the total Price. Make sure the program accepts only nonnegative input.
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
 #include<iostream>
using namespace std;
int main()
{
	const double price = 12.00;
	double newprice = 0;

	int amount;

	cout << "How many shirts would you like?"
		cin >> amount;

	if(amount >=5 && amount <=10 )
		newprice = price - (price * 0.1)
		
	else if (amount >= 11 && amount <= 20)
		newprice = price - (price * 0.15)
		
	else if (amount >= 21 && amount <= 30)
		newprice = price - (price * 0.2)
		
	else if (amount >= 31)
		newprice = price - (price * 0.25)
		
	else
		newprice = price;



	return 0;





}
Last edited on
What specifically do you need help on? From this point one would assume you need to calculate and print the final price back to the user.

Edit: I see parts missing from your code such as handling negative inputs, but I am not sure what about it confuses you. I also suggest using brackets in your if statement. Even though it is readable here, the brackets will make everything more readable in the greater scheme of things.

Edit 2: I just noticed your lack of semicolons. That would actually be a good start.
Last edited on
closed account (D4NbpfjN)

This my current code. when i run it and type how many shirts it does not do the calcultion.

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
42
43
44
45
46
#include<iostream>
using namespace std;
int main()
{
	const double price = 12.00;
	double newprice = 0;

	int amount;

	cout << "How many shirts would you like?";
		cin >> amount;

	if (amount >= 5 && amount <= 10)
	{
		newprice = price - (price * 0.1);
	}
		
	else if (amount >= 11 && amount <= 20)
	{
		newprice = price - (price * 0.15);
	}
		
	else if (amount >= 21 && amount <= 30)
	{
		newprice = price - (price * 0.2);
	}
		
	else if (amount >= 31)
	{
		newprice = price - (price * 0.25);
	}
		
	else
	{
		newprice = price;
	}
	


	return 0;





}
You are meant to calculate multiple things in this problem. Try to break the problem up like this:

-get value from the user
-check that user value is valid
-change price based on discount
-calculate total cost for customer
-print final price to the customer

While your code doesn't have errors, there are multiple steps to the problem that are not implemented at all. You can program this very linearly, so just step through and add the parts you don't have.
Last edited on
closed account (D4NbpfjN)
im a little stuck on this. trying to figure out how to calculate it correctly.
closed account (D4NbpfjN)
I am having trouble with the calculations for this code.
Why do you think it's not doing the calculation? Perhaps you ought to put something in the code that outputs the result of the calculation, so that you can be sure?
PrivateRyan gave you 5 steps you need to follow. You've done #1 and #3. You're missing #2, #4 and #5.

#2: Use a while loop.
1
2
3
4
    while (amount < 0)
    {  cout "# of shirts invalid" << endl;
        cin >> amount;
    }


#4: Calculate the total cost for the customer.
1
2
3
    double cost; 
...
    cost = newprice * amount;


#5: Print the final price to the customer
 
  cout <<  "The cost of " << amount << " shirts is: " << cost << endl;


Perhaps one of the reasons you're struggling is that amount is not a very good name for the number of shirts purchased. Amount usually implies a dollar amount. quantity or num_shirts would have been a better choice.
closed account (D4NbpfjN)
everything is working on my code except two things now.
closed account (D4NbpfjN)
This is what i need help with now.
Here is my code so far. It is doing what i want it to except two things. The first one being when i enter the negative number it shows up invalid input what i wanted it to do, but it also shows up the retail price of the shirt and the final amount and it was -24. How do i get it not to do that. Another one is that if i type 8 shirts it comes up the regular retail price of 12 dollars instead of 10.80. If you look below this is the sample run i am suppose to be going by.

Sample Run 1:
How many shirts would you like?
4
The cost per shirt is $12 and the total cost is $48
Sample Run 2:
How many shirts would you like?
0
The cost per shirt is $12 and the total cost is $0

Sample Run 3:
How many shirts would you like?
8
The cost per shirt is $10.80 and the total cost is $86.40

Sample Run 4:

How many shirts would you like?
-2
Invalid Input: Please enter a nonnegative integer




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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
	const double price = 12.00;
	double newprice = 0;
	double discount1 = 0.1;
	double discount2 = 0.15;
	double discount3 = 0.2;
	double discount4 = 0.25;

	int shirts;

	cout << "How many shirts would you like?";
		cin >> shirts;
		
		if (shirts < 0)
			cout << "Invalid Input: Please enter a nonnegative integer\n";

		if (shirts >= 5 && shirts <= 10)
		{
			newprice = price - (price * discount1);
			newprice *= shirts;
		}

		else if (shirts >= 11 && shirts <= 20)
		{
			newprice = price - (price * discount2);
			newprice *= shirts;
		}

		else if (shirts >= 21 && shirts <= 30)
		{
			newprice = price - (price * discount3);
			newprice *= shirts;
		}

		else if (shirts >= 31)
		{
			newprice = price - (price * discount4);
			newprice *= shirts;
		}

		else
		{
			newprice = price;
			newprice *= shirts;
		}
	
		
		cout << "The retail price per shirt is $" << price << endl;
		cout << "The new price of one shirt is $" << newprice << endl;


	return 0;





}



The first one being when i enter the negative number it shows up invalid input what i wanted it to do, but it also shows up the retail price of the shirt and the final amount and it was -24.

That's because you used an if statement, not a while loop, as I suggested above. Reread what I posted regarding #2 above. Your if statement executes, but you continue as if everything is okay.

Lines 21-49: In each if statement, you're using newprice for two different things. The discounted price and the total cost. Not a good idea. Better style to introduce a new variable for total_cost, than to use newprice for two different things. Note that at line 53, you're not displaying the discounted price of one shirt because you overwrote newprice.


Last edited on
closed account (D4NbpfjN)
This is my current code. I have an error on line 22 saying that the cost uninitialized local variable 'cost' used.
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using namespace std;
int main()
{
	const double price = 12.00;
	double newprice = 0;
	double cost;
	double discount1 = 0.1;
	double discount2 = 0.15;
	double discount3 = 0.2;
	double discount4 = 0.25;

	int shirts;

	cout << "How many shirts would you like?";
		cin >> shirts;
		
	

		if (shirts >= 5 && shirts <= 10)
		{
			newprice = price - (price * discount1);
			cost *= shirts;
		}

		else if (shirts >= 11 && shirts <= 20)
		{
			newprice = price - (price * discount2);
			cost *= shirts;
		}

		else if (shirts >= 21 && shirts <= 30)
		{
			newprice = price - (price * discount3);
			cost *= shirts;
		}

		else if (shirts >= 31)
		{
			newprice = price - (price * discount4);
			cost *= shirts;
		}

		else
		{
			newprice = price;
			cost *= shirts;
		}
		while (shirts < 0)
		{
			cout << "Invalid Input : Please enter a nonnegative integer" << endl;
			cin >> shirts;
		}
		
		cout << "The retail price per shirt is $" << price << endl;
		cout << "The new price of one shirt is $" << newprice << endl;


	return 0;





}
Last edited on
I have an error on line 22 saying that the cost uninitialized local variable 'cost' used.

Line 22: Your're using the *= operator which says to multiply cost by shirts and store the result in cost.. What's the value of cost before the operation? Hint garbage.

Also, that's not the formula you want to use. What you want is:
1
2
 
    cost = newprice * shirts;


Lines 48-52: These lines are in the wrong place. You want to test the value of shirts after you input it and BEFORE you use it. Move these lines to after line 15.

Line 56: You're not displaying the total cost.

Lines 22, 28, 34, 40, 46: These formulas are all the same. After you correct the formula, you can do the calculation once at line 53.


Last edited on
closed account (D4NbpfjN)
okay thank you. everything on the code is working except for one thing. In the above problem if i enter 8 shirts it is suppose to calculate 10.80 per shirt and the total being 86.40. I have the total correct, but the cost per shirt is just $12. How should i change it so when i enter 8 shirts it gives me $10.80 per shirt?
Works correctly for me:


How many shirts would you like?8
The retail price per shirt is $12
The new price of one shirt is $10.8
The total cost is: 86.4
Press any key to continue . . .


Post your current code.
closed account (D4NbpfjN)
i got it to work correctly. thank you for your help
Just going back to the excellent advice PrivateRyan gave you much earlier:

PrivateRyan wrote:

-get value from the user
-check that user value is valid
-change price based on discount
-calculate total cost for customer
-print final price to the customer


That could be construed as pseudo-code, and it could be a very useful thing - especially if one is starting out.

You can put that text into a cpp file as comments, then go back and either add more detailed comments, or write the code which carries out each particular thing. That way, no matter how complex the problem is, one can always has a framework to start with. Note that even the simplest program is going to consist of :

1
2
3
4
// get input - from keyboard or file
// validate input
// process / calculate
// Display output to screen or file 


Doing pseudo-code can help identify where loops need to be, what needs to go in functions, keeps your thinking logical and organised. The comments can be left in to serve a kind of documentation, but it's better to have meaningful names for functions and variables to self document the code without a bunch of useless comments. Comments can be used to describe things like pre and post conditions (such as valid ranges of expected values), and unusual situations.
Topic archived. No new replies allowed.