- 1

ā€œWho Doesnā€™t Love a Discount?ā€ (Do...while)

A supermarket has launched a campaign offering a 15% discount on 3 items. Write a program that takes the total price for every purchase as input and outputs the relevant discount. Sample Input 15000 4000 6700 Sample Output 2250 600 1005 Explanation 2250 represents a 15% discount for a 15000 purchase; 600 for 4000 and 1005 for 6700 accordingly. https://code.sololearn.com/cQd6RpWHZTMY/?ref=app

28th Feb 2021, 1:15 AM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar
14 Answers
+ 6
Hai TahitišŸ·... I corrected your errors in your coding... Check it https://code.sololearn.com/cPnLf0m3g4BR/?ref=app
28th Feb 2021, 5:51 AM
K.Suhanya
K.Suhanya - avatar
+ 5
Yes, I forgot about that totally. Maybe you can do this without array at all. Look at this pseudo and try to implement it in code. Let <i> be 0 Begin loop Read value for <purchase_amount> Print 15% of <purchase_amount> Increment value of <i> While <i> < 3
28th Feb 2021, 4:57 AM
Ipang
+ 3
TahitišŸ· It's okay, don't mind it. You have got a working solution after all šŸ‘
1st Mar 2021, 3:35 AM
Ipang
+ 2
Here is my code for the problem, it starts by printing the 3 values correctly, but it just keeps printing the last value over and over again. Anyone know a solution for this? #include <iostream> using namespace std; int main() { int purchaseAmount = 0; int totalPrice; //your code goes here do{ cin >> purchaseAmount; totalPrice = purchaseAmount * 0.15; cout << totalPrice << endl; } while(purchaseAmount >= 3); return 0; } EDIT: I was able to fix it with these changes: #include <iostream> using namespace std; int main() { int purchaseAmount = 0; double totalPrice; //your code goes here do{ cin >> totalPrice; purchaseAmount = purchaseAmount + 1; totalPrice = totalPrice * 0.15; cout << totalPrice << endl; } while(purchaseAmount < 3); return 0; }
12th Jun 2021, 9:02 AM
Otso
+ 1
Consider to save the 3 purchase into an array first (use a loop). Once the 3 purchase had been stored in array, then output the 15% discount of each array element also using a loop.
28th Feb 2021, 3:13 AM
Ipang
+ 1
Ipang , I appreciate your help, although I did not understand the ā€œpseudoā€.
28th Feb 2021, 9:23 PM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar
0
I donā€™t understand whatā€™s wrong with my Line 11.
28th Feb 2021, 1:16 AM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar
0
Ipang , Iā€™m only on level 7, almost finished learning about Conditionals and Loops. I donā€™t know why the module would present a quiz question in which ā€œarrayā€ must be used, without having taught yet about how to code an array. Is this the only way to code the program correctly? Thank you.
28th Feb 2021, 4:44 AM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar
0
TahitišŸ· Why did you add the post increment along with cin? I don't see a point in it, if it ever worked (as cin and ++ cannot be used simultaneously in C++). Var++ returns something else; not a variable. ++ only works if the value isn't changed before the increment happens. This means that you cannot simplify the code that way. Try: cin >> var; var++; Let me know if I've typed anything incorrect.
28th Feb 2021, 8:28 AM
Calvin Thomas
Calvin Thomas - avatar
0
K.Suhanya , Thank you. This worked. What I did (per your advice), was switch the cin and cout lines, then changed the (>= ) operator to simply <. But I donā€™t understand why I had to use >. I understood ā€œpurchaseAmountā€ as the number of items purchased. The customer must purchase at least 3 items. My logic therefore, was that while the purchase amount (number of items) is greater than or equal to 3, I want the program to output a 15% discount on the total price. šŸ¤·šŸ½ā€ā™€ļø
28th Feb 2021, 9:29 PM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar
0
Calvin Sheen Thomas , I tried everyoneā€™s advice in the order it was given. Iā€™m very new to C++ (and coding in general), so I am not familiar with all the concepts, including ā€œvar.ā€ I didnā€™t mention this before, but at the bottom of the instructions for this exercise, was the following: ! Use cin inside the loop to get an input for every iteration. I used purchaseAmount++ because I thought that I must increment the number of items this way, so that a minimum of three items would eventually be represented by the total price. I donā€™t know if that was the correct way to reason, but it actually worked.
28th Feb 2021, 9:38 PM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar
0
Ipang , šŸ‘šŸ˜ƒ
1st Mar 2021, 3:40 AM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar
0
#include < iostream> using namespace std; int main() { int purchaseAmount = 0; int totalPrice; do { cin >> totalPrice; cout << ( totalprice * 0.15) << endl; purchaseAmount = purchaseAmount + 1; } return 0; } šŸ‘šŸ‘ [15% = 15/10 = 0.15]
20th Nov 2021, 1:36 PM
Srinivas vedullapalli
Srinivas vedullapalli - avatar
0
#include <iostream> using namespace std; int main() { float purchaseAmount = 0; float totalPrice; int item = 0; do{ item += 1; cin>> purchaseAmount; totalPrice = purchaseAmount*0.15; cout<<totalPrice<<endl; } while(item < 3); //your code goes here return 0; }
10th Dec 2021, 2:28 PM
David Zippel
David Zippel - avatar