- 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
14 Answers
+ 6
Hai Tahitiš·...
I corrected your errors in your coding...
Check it
https://code.sololearn.com/cPnLf0m3g4BR/?ref=app
+ 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
+ 3
Tahitiš·
It's okay, don't mind it. You have got a working solution after all š
+ 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;
}
+ 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.
+ 1
Ipang , I appreciate your help, although I did not understand the āpseudoā.
0
I donāt understand whatās wrong with my Line 11.
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.
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.
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. š¤·š½āāļø
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.
0
Ipang , šš
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]
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;
}