0

This is a change counter program. The change is counting correctly, but the remainder is not (remChange()). After watching a few videos, and w3schools tutorials, I'm still coming up blank as to why this is. I've added a returnChange at the bottom of each function and was able to get that last 2 functions rounded (from the float). Did I do the floating point wrong in the println? Are my return tags in the wrong place? I'm just trying to get it to work by counting the coins and giving me back the change.

Here is my code: let me know, ill be researching in the meantime.

fun main(args: Array<String>) {
    var  change = 10.88

    getDollars(change)
    getQuarters(change)
    getDimes(change)
    getNickles(change)
}

fun getDollars(change: Double): Double{
    val numofdolla = change/(1.00).toFloat();
    println(numofdolla.toInt());
    val remChange =  change - numofdolla * 1.00;
    println(remChange)
    return change
}

fun getQuarters(change: Double): Double{
    val numofqtr = change/(0.25).toFloat();
    println(numofqtr.toInt());
    val remChange = change - numofqtr * 0.25;
    println(remChange)
    return change
}

fun getDimes(change: Double): Double{
    val numofdime = change/(0.10).toFloat();
    println(numofdime.toInt());
    val remChange =  change - numofdime * 0.10;
    println("%.2f".format(remChange))
    return change
}

fun getNickles(change: Double): Double{
    val numofnick = change/(0.05).toFloat();
    println(numofnick.toInt());
    val remChange = change - numofnick * 0.05;
    println("%.2f".format(remChange))
    return change
}

I'm not looking for anyone to write my code, as an explanation would be just perfect.

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • Why are you calculating in dollars (which needs floating-point values that are inherently inaccurate for representing decimal fractions), when you could calculate in cents and use precise integers? (Or, if you really need dollars for some reason, you could use `BigDecimal`s). – gidds Dec 04 '21 at 00:03
  • I didn't even know that was a thing! Thank you! I don't know. I'm teaching myself on the internet so I can't know what i don't know, but ill look into precise integers. – MissMacDaddy Dec 04 '21 at 00:04
  • ‘precise’ was a description, not a type name :-) I just meant you can use `Int` (or the other integer types `Long`, `Short`, &c), which has no issues with inaccuracy or rounding. Floating-point causes much confusion, and many people use it where it's really not appropriate; [this question](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) is probably the most popular one on the subject. – gidds Dec 04 '21 at 00:14
  • so, if I understand correctly, are you telling me that toFloat is causing the issue of my output not returning how it should? – MissMacDaddy Dec 04 '21 at 00:19
  • I haven't debugged your code. But you're storing money values in floating-point, and [that's](/questions/3730019) [almost](https://dzone.com/articles/never-use-float-and-double-for-monetary-calculatio) [always](https://husobee.github.io/money/float/2016/09/23/never-use-floats-for-currency.html) [a](https://spin.atomicobject.com/2014/08/14/currency-rounding-errors/) [bad](https://newbedev.com/why-not-use-double-or-float-to-represent-currency) [idea](https://blog.agentrisk.com/-2edb52cdf308)…  So I'd suggest changing it to work in cents, not dollars. (And if it still fails, post a new question.) – gidds Dec 04 '21 at 00:27
  • I got rid of the float except for the last 2 functions. – MissMacDaddy Dec 04 '21 at 00:46

1 Answers1

1

I'm not sure what you're expecting to happen here, but the title of your question seems to suggest that you expect the change variable to change when calling those helper functions.

You cannot mutate a function argument within a function in Kotlin, but you can indeed return a new value from the function. It seems you tried to do that, but here your main function is not using the return values of the getXxxx functions. So your change variable is never updated.

You can update the change variable based on the result of those functions:

fun main(args: Array<String>) {
    var change = 10.88

    change = getDollars(change)
    change = getQuarters(change)
    change = getDimes(change)
    change = getNickles(change)
}

That said, those functions currently return the original change variable (which is unchanged), so if you want the value to change, you probably want to return remChange instead.

Joffrey
  • 32,348
  • 6
  • 68
  • 100
  • ok so this was my same though as well, but it gives me this error "expecting a top level declaration. Now, would it be easier to put the remChange calculation in its own function or is it ok the way i have it? – MissMacDaddy Dec 04 '21 at 17:51