# Problem with pset 1, cash

I get the correct answer for some values, i get the wrong answer for others. I didn't convert my change to cents because I figured I could just keep all my cents, dimes, quarters in decimal values too. I think my issue arises when the value equals one of the conditions in the loops and then the coin value won't increase. Anyone can tell me what's wrong with this code ?

``````#include <cs50.h>
#include <stdio.h>
int main(void)
{
float change;
do
{
change = get_float("change owed = ");
} while (change < 0);

int coins = 0;
while (change >= 0.25)
{
coins ++;
change = change - 0.25;
}

while (change >= 0.10)
{
coins ++;
change = change - 0.10;
}

while (change >= 0.05)
{
coins ++;
change = change - 0.05;
}

while (change >= 0.01)
{
coins ++;
change = change - 0.01;
}
printf ("%i\n", coins);
}
``````

You’ve run into float imprecision, which is the main lesson of this problem. The specialization and walkthrough recommended that you multiply the user’s input by 100 and then round it, (there is a function you can use to do that which you can find at the bottom of the specification. ) Then you can just conveniently use whole numbers in your while loops.

• Float imprecision is the term computer scientists use to describe how floating point values aren’t perfect. For example, the float 5.00 isn’t exactly equal to 5.0000000...(keep on adding 0 forever). It’s instead equal to something like 5.0000000001538492. So after a couple of zeros, the value of the float becomes imprecise / unexact. If you’re still confused, check out this link: docs.microsoft.com/en-us/cpp/build/reference/… Jan 3 '19 at 5:26

Use int cash = round(change * 100); And then change all float to int like 0.25 to 25

In addition to the float imprecision, every "while" loop in your code evaluates to true as the owing amount will always be greater than 1 cent, 5 cents, 10 cents, and 25 cents if it is greater than 25 cents. Try using AND (&&) operator in the while loops to ensure the correct coin is used, as follows:

``````#include <stdio.h>
#include <cs50.h>
#include <math.h>

int main(void)
{
float dollar;
int cent;

// Prompts the user to input the amount owed (in dollars)
do
{
dollar = get_float("Please enter the amount: ");
cent = round(dollar * 100);
}
while (dollar < 0.0);

// Initialize the number of coins (zero initially)
int coins = 0;

// Increments the coins count if the owed amount is greater than 25 cents
while (cent >= 25)
{
coins++;
cent = cent - 25;
}

// Increments the coins count if the owed amount is less than 25 cents and greater than 10 cents
while (cent >= 10 && cent < 25)
{
coins++;
cent = cent - 10;
}

// Increments the coins count if the owed amount is less than 10 cents and greater than 5 cents
while (cent >= 5 && cent < 10)
{
coins++;
cent = cent - 5;
}

// Increments the coins count if the owed amount is less than 5 cents and greater than 1 cent
while (cent >= 1 && cent < 5)
{
coins++;
cent = cent - 1;
}

// Prints the number of coins used to return the change
printf("Minimum number of coins required: %i\n", coins);
}
``````
``````#include<cs50.h>
#include<math.h>
int main()
{    // to prompt for input

float i;
do
{
i = get_float("change owed : ");
}
while (i < 0);

// for rounding up to an integer

int j = round(i * 100);

// mathematical calculations

int k = j % 25;
int l = (j - k) / 25;

int m = k % 10;
int n = (k - m) / 10;

int o = m % 5;
int p = (m - o) / 5;

// totaling the number of coins

int c = l + n + p + o;

printf("%i \n", c);
}
``````
• You should never ever share the whole solution to a problem when giving an answer until and unless it is absolutely necessary. Dec 22 '20 at 13:00