- 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 Antworten
+ 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;
}