0

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
}}}}

1 Answer 1

0

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).

7
  • For those of you following along at home: I had no idea how to visualize the phrase "initialized the variable"). Here's a link with a great example : stackoverflow.com/questions/7975008/initializing-variables-in-c
    – SuperZumo
    Jan 8, 2018 at 23:25
  • Alright, so you're saying that this script doesn't work if c < 24. I know. I'm taking baby steps. First I want to make sure any value over 24 works as advertised. I'm trying to get this freaking greedy crap algorithm working in a proof of concept. so you say that I haven't initialized the variable "change" but I do 2 rows below c > 24 -----> change = c % 25. Why does this script only work with 35 as the value of c ?
    – SuperZumo
    Jan 8, 2018 at 23:32
  • 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.
    – Blauelf
    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.
    – Blauelf
    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.
    – Blauelf
    Jan 9, 2018 at 9:15

You must log in to answer this question.

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