Any helpful info calculating a percent in a do-while loop | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Any helpful info calculating a percent in a do-while loop

The do-while loop challenge everyone loves a discount. I'm doing a calculation totalPrice*discount (discount=0.15) //or 15%. I keep getting 0 first of all and just generally running into walls with this one. I feel like i'm missing something!

12th Oct 2020, 7:58 PM
Mandy
Mandy - avatar
6 Answers
+ 1
Mandy It's recommended to save the code in CodePlayground and insert the link in the description instead of pasting it here. I'll do it for you this time but take it into account in the future: https://code.sololearn.com/cb57juAp631m/?ref=app
12th Oct 2020, 9:31 PM
Kevin ★
+ 4
You need to be careful that you are not getting integer division when you need to divide two floats. If necessary, cast to float using a (float) expression immediately before each variable - see https://stackoverflow.com/questions/5456801/c-int-float-casting Edit: https://www.w3adda.com/cplusplus-tutorial/cpp-type-casting may also be very helpful.
12th Oct 2020, 8:44 PM
Jonathan Shiell
+ 2
As Jonathan Shiell suggested your "discount" and "sum" variables should be of "float" or "double" types because they store floating point numbers. (this isn't casting) Line 16: (sum>0) does nothing and you can get rid of it while (purchaseAmount >= 3); Above line means that the loop body will run until purchaseAmount is not equal or greater than 3. purchaseAmount is ALWAYS 3, you don't change this value, so the loop will run infinitely. If you want the loop to run 3 times you should modify the condition and change the value of purchaseAmount within the loop. Let's decrement it by one on each iteration and break the loop when it equals 0: do { ....... purchaseAmount -= 1; } while (purchaseAmount > 0)
12th Oct 2020, 9:49 PM
Kevin ★
+ 1
This is the challenge link: https://www.sololearn.com/coach/268?ref=app Share your complete code so we can help
12th Oct 2020, 8:22 PM
Kevin ★
+ 1
just to clean up my sloppy posts earlier, and for practice's sake: https://code.sololearn.com/c3I4o8Z1T5KI it contains the right output now except it's just repeating forever, this is where I'm going to have a look at what you suggested Kevin thank you. (sum>0) has been removed And also thanks again Jonathan those links you posted contain helpful info for review!
12th Oct 2020, 10:30 PM
Mandy
Mandy - avatar
0
I know that code is incredibly wrong, thank you for the resources ill check them out. Im very new at this so i appreciate the support!
12th Oct 2020, 9:18 PM
Mandy
Mandy - avatar