# Pset1 cash. Please help me understand my script. Only works for 35 cents

I am working on pset1 cash.

I'm taking it step by step trying to work out the issues so I know the logic isn't complete but I would really like someone's input explaining why my script only works when I input 35 cents (".35") .

If I try anything else it doesn't return any number of coins. In theory it should work with any value that doesn't have pennies and higher than 24 cents.

As always, I appreciate everyone's help and its helping a bunch.

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

int main (void)

{
float n;

//do-while loop to prompt user until you get a positive dollar amount.
do

{
n = get_float("Enter the change you will give me: ");
}

while (n < 0);

float  x = round(n*100);
int c = x;

int quarters;
int change;
int dimes;
int rdimes;
int nickels;
int rnickels;

if ( c > 24)

{
quarters =(int)floor(c / 25);  //rounding down to get # of quarters
change = c % 25;               //value represents remainder after module
// by quarters

if ( change > 9)
{
dimes =(int)floor(change / 10);  //rounding down to get # of dimes
rdimes = change % 10;                 //value represents remainder after
// modulo by dimes

if (rdimes > 4)
{
nickels =(int)floor(rdimes / 5); //rounding down to get # of nickels
rnickels = rdimes % 5;                //value representds remainider
//after moduly by nickels

printf("%i" , (quarters + dimes + nickels));  //print out the sum of the
//number of quarters, dimes, nickels
}}}}
``````

If not `c > 24`, variable `change` would stay at its initial value (which is undefined, as you haven't initialised the variable). What keeps you from reusing `c` all the time? (or pick a longer variable name)
BTW, no need to `floor` if you divide integers. Integer division always truncates (rounds towards zero).
• That it works for 35 is pure coincidence, as `nickels` in this case never receives a value, and could be anything. It's most likely 0, but that's by no means guaranteed. For 40 it does the right thing, also 65, 90, and so on. Jan 9, 2018 at 9:09
• Also, your `change = c % 25;` only happens for values `c > 24`, if you have less, that money never gets passed to `change`. If you for example did `quarters = c / 25; change = c % 25;` without an `if`, it would always assign a value. Jan 9, 2018 at 9:12
• I'd use something like `coins += c / 25; c %= 25;` instead, as I'm not really interested in different kinds of coins or the remaining money after a specific step. Jan 9, 2018 at 9:15