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.
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.
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.
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.
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
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.
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.
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?
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 . . .
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.