0

When I take the user input in dollars (as a float) and multiply it by 100 -- even when I then save that value as an intermediate float "cents" before changing it to an integer, for some reason the output makes the user input value into an integer before multiplying it by 100 to change into cents.

For example, on user input 3.87, the output is 300 cents, or 12 coins, instead of 387 cents and 18 coins.

My (relevant) code is as follows:

float cents = (100 * get_change()); 
int c = round(cents);

...

int get_change(void)
{
    float d;
    do
    {
        d = get_float("Change Owed (in dollars): \n");
    }
    while (d < 0);
    return d;
}
0

The problem is your return type from get_change(). Even though d is a float, the return type is an int, so d is cast as an int, the change is truncated, and only the whole dollar amount is returned, as an int.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

| improve this answer | |
  • How do I get my return type to be a float? I put in "get_float" and defined d as a float? – Matthew Jun 19 at 2:07
  • oh I figured it out. I had to define my function as float get_change(void) – Matthew Jun 19 at 2:09
  • We have a winner!!! ;-) – Cliff B Jun 19 at 3:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .