# Thread: Help with a problem (Im new to this)

1. ## Help with a problem (Im new to this)

Hi everyone, I have decided to start a course on programming called CS50, and one of the tasks Im asked to do is program in gedit using C, a simple program that asks for the change owed to the customer and then gives back the number of coins needed to satisfy said change. Nevertheless, the cashier is greedy, and likes to have many coins, so he has to give the bigger amount coins first, starting with 0.25, then 0.10 then 0.05 and then 0.01.

For example, if I input that the change owed is 0.41, the program must return a value of 3 coins, a 0.25, a 0.05 and a 0.01.

I have written the following:
insert
Code:
```#include <stdio.h>
#include <cs50.h>

int main(void)
{
float change;
int coins;
coins = 0;
printf("How much change is owed\n");
change = GetFloat();
for (;change>0.00;)
{
if (change>=0.25)
{
change-=0.25;
coins+=1;
}
else if (change>=0.10 && change<0.25)
{
change-=0.10;
coins+=1;
}
else if (change>=0.05 && change<0.10)
{
change-=0.05;
coins+=1;
}
else if (change>=0.01 && change<0.05)
{
change-=0.01;
coins+=1;
}
}
printf("%d\n",coins);
}
```

But for some reason it is answering to the numbers divisible by 0.25, as soon as i say 0.41, or 0.01 it just jumps a line and does nothing.

Please help me solve this problem not by offering me a better code completely, but by working from this code. Thanks in advance I really am new to this so please bear with my negligence xD 2. My preference would be to multiply everything by 100 and do everything in pennies. If you do it in pennies, then you can use the % (modulus) operator, and it becomes much simpler. 3. Originally Posted by Elkvis My preference would be to multiply everything by 100 and do everything in pennies. If you do it in pennies, then you can use the % (modulus) operator, and it becomes much simpler.
Ok I've changed it and now it works for al the values I try except for 4.2. I've read that 4.2 as a float is 4.1999999999 and when converting it into an int it becomes 419, so how can I fix this using the code I have because its just one damn value. Here is the code:

insert
Code:
```#include <stdio.h>
#include <cs50.h>

int main(void)
{
int coins;
int change25;
int change10;
int change5;
int intchange;
int change1;
coins=0;
printf("How much change is owed\n");
intchange = GetFloat()*100;
if (intchange>0)
{
if (intchange/25>=1)
{
change25 = intchange/25;
coins+=change25;
intchange=intchange%25;
}
if (intchange/10>=1)
{
change10 = intchange/10;
coins+=change10;
intchange=intchange%10;
}
if (intchange/5>=1)
{
change5 = intchange/5;
coins+=change5;
intchange=intchange%5;
}
if (intchange/1>=1)
{
change1 = intchange/1;
coins+=change1;
intchange=intchange%5;
}
printf("%d\n",coins);
}
}```
Thanks in advance  4. You could try a double instead, but I suspect that will have the same effect.

Another option could be to do something like this to get your initial amount:

Code:
```int dollars = 0, pennies = 0;
do
{
printf("Enter amount: ");
int result = scanf("%d.%2d", &dollars, &pennies);
} while (result != 2);
intchange = (dollars * 100) + pennies;``` 5. Try this
Code:
`intchange = GetFloat()*100+0.5`
to round it to nearest cent. Popular pages Recent additions 