1. ## Count problem

Hi,

I am required to write a program that counts the minimum amount of coins required to return a change, i.e. if user inputs .57 for example, the program has to output number 5 (two quarters, one dime, one nickel).

What I have so far is this (

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

int main(void)
{
//creates a infinite loop that checks if the value is valid
while(1)
{
//get a float value from the user and convert it to cents
printf("O hai! How much change is owed?\n");
float v = GetFloat();
v = v*100;
int c = (int) v;

//quarters
if(c >= 25)
{
int count = 0;
count = (c / 25);
c = c % 25;
printf("%d\n", count);

}

//dimes
if(c >= 10)
{
int countd = 0;
countd = count + (c / 10);
c = c % 10;
printf("%d\n", countd);

}
break;

}
return 0;
}```
Quarters part works as it should (If I remove the dimes part, compile it, run it and input .50 for example, it spits out 2), however what I do not understand is why I cannot use the "count" from quarters and then add dimes to it (c / 10).

If I try to compile it like this I get an error message saying that

Code:
`greedy.c:41:13: error: use of undeclared identifier 'count'; did you mean 'countd'?`
Any help greatly appreciated. Thank you 2. Variables declared within braces are only valid within those braces. What you have is basically like this:

Code:
```{
int a = 5;
}

{
int b = a + 1;  // not correct because a is out of scope now
}``` 3. And how should I solve this problem then? 4. Actually nevermind, I found the solution  5. Hi, so it's me again!

So I managed to finish whole code, it works as it should for 99% of values, however, for some values it just keeps going on and on in an infinite loop and I have no idea why

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

int main(void)
{
//creates a infinite loop that checks if the value is valid
while(1)
{
//get a float value from the user and convert it to cents
printf("O hai! How much change is owed?\n");
float v = GetFloat();
v = v*100;
v = round(v);
int c = (int) v;
int count = 0;

if(c > 0)
{
//quarters
while(c >= 25)
{
count = (c / 25);
c = c % 25;

}

//dimes
while(c >= 10)
{
count = count + (c / 10);
c = c % 10;
}

//nickels
while(c >= 5)
{
count = count + (c / 5);
c = c % 5;
}

//pennies
while(c >= 1)
{
count = count + (c / 1);
}

//prints out the final count
printf("%d\n", count);

break;
}
}
return 0;
}```
Any ideas? //Actually I see it now, if the value is less then 25, then program doesn't understand "count + (c / 10)". Any ideas how could I solve this? 6. Code:
```while(c >= 1)
{
count = count + (c / 1);
}```
never changes c so, if the loop body is executed once, it will be executed an infinite number of times. 7. Yup, you're right! Thank you very much  8. Line 24 isn't right either. The second and subsequent time around the loop the value of count that was previously calculated is overwritten.
Edit: Scratch that. The last four while loops are completely redundant. They never loop more than once, and may as well just be if-statements. But even then, there's no harm in just executing those statements inside the block anyway.
You would use a while loop only if you were going to subtract the amount of one coin each time.

Also note that a = a + b; can be written as a += b; 9. .57 for example, the program has to output number 5 (two quarters, one dime, one nickel).
Shouldn't it be 2 quarters, 2 pennies and 1 nickel? Granted it's still 5 coins, but don't lose your job at the register! 10. Originally Posted by whiteflags Shouldn't it be 2 quarters, 2 pennies and 1 nickel? Granted it's still 5 coins, but don't lose your job at the register!
Yeah well, it was one of the mistakes I made previously, I thought that nickel is 2 cents, not 5. I am an European so I dunno this stuff @iMalc: Thank you Popular pages Recent additions 