Hey guys im in drastic need of help. Please fine below my code for the conversion of change in dollars and cents. The program has to convert the real number into 2 ints then output how much of each denomination to give. As is the code works fine for the dollars however for some reason cents is always 0 so im not sure if there is an issue that a guru may be able to see where im going wrong.
Thanks in advance.

#include <stdio.h>

/* Declare function prototypes */
void Welcome ();
float GetAmount ();
void Convert (float, int&, int&);
void DollarValues (int, int&, int&, int&, int&, int&, int&, int&);
void CoinValues (int, int&, int&, int&, int&);
int RepeatedlyDollars (int&, int);
int RepeatedlyCoins (int&, int);
void PrintChange (int, int, int, int, int, int, int, int, int, int, int, int, int);

/* This module is just to inform the user of what the program does and a few rules to enter valid numbers. */
void Welcome ()
    printf ("Welcome to the change calculator.\nThis code will work out how much change in to give out.\n");
    printf ("Your change will need to be entered in the correct demoninations.\n");
    printf ("For DOLLARS are 100, 50, 20, 10, 5, 2, 1 and any denominatiion of these.\n");
    printf ("For CENTS a value between 5 - 95 in denominations of 5 is required.\n");


/* This module is where the user will enter their value in cents */
float GetAmount (void)
    float change;

    /* Prompt user for input. */
    printf ("Please enter your amount in DOLLARS & CENTS:\n");
    scanf ("%f%*c", &change);

    return (change);

/* This module needs to convert the float into two seperate integers */
void Convert (float change, int &dollars, int ¢s)

    dollars = (int) change;
    cents = (int) (((change - dollars)*100) + 0.5);

    /* Test for invalid input, if invalid print error message */
    if (cents%5 > 0)
        printf ("Sorry, that's not a valid amount. Please enter a valid amount.\n");

void DollarValues (int dollars, int &OneHundredDollars, int &FiftyDollars, int &TwentyDollars, int &TenDollars, int &FiveDollars, int &TwoDollars, int &OneDollars)
    int onehundreds = 100;
    int fiftys = 50;
    int twentys = 20;
    int tens = 10;
    int fives = 5;
    int twos = 2;
    int ones = 1;

    OneHundredDollars = RepeatedlyDollars (dollars, onehundreds);
    FiftyDollars = RepeatedlyDollars (dollars, fiftys);
    TwentyDollars = RepeatedlyDollars (dollars, twentys);
    TenDollars = RepeatedlyDollars (dollars, tens);
    FiveDollars = RepeatedlyDollars (dollars, fives);
    TwoDollars = RepeatedlyDollars (dollars, twos);
    OneDollars = RepeatedlyDollars (dollars, ones);


/* This function sets how much eash coin is worth and calls the Repeatedly function to calculate how many of each coin are needed */
/* Use pass by reference to get all the results out */
void CoinValues (int cents, int &FiftyCentCoins, int &TwentyCentCoins, int &TenCentCoins, int &FiveCentCoins)
    /* These variables represent coin values that are needed for calculation */
    int fifty = 50;
    int twenty = 20;
    int ten = 10;
    int five = 5;

    /* The number of each type of coin is calculated by Repeatedly */
    FiftyCentCoins = RepeatedlyCoins (cents, fifty);
    TwentyCentCoins = RepeatedlyCoins (cents, twenty);
    TenCentCoins = RepeatedlyCoins (cents, ten);
    FiveCentCoins = RepeatedlyCoins (cents, five);


int RepeatedlyDollars (int &dollars, int dollar_worth)
    int notes;

    notes = (dollars/dollar_worth);
    dollars = (dollars % dollar_worth);

    return (notes);

/* This function is used to calculate how many of each coin is needed */
int RepeatedlyCoins (int ¢s, int coin_worth)
    int coins;

    /* This block calculates the number of coins needed & makes the remainder = to the new cents value */
    coins = (cents/coin_worth);
    cents = (cents % coin_worth);

    return (coins);

/* This function will print the amount of notes and coins needed for the user entered change value */
void PrintChange (int dollars, int cents, int OneHundredDollars, int FiftyDollars, int TwentyDollars, int TenDollars, int FiveDollars, int TwoDollars, int OneDollars, int FiftyCentCoins, int TwentyCentCoins, int TenCentCoins, int FiveCentCoins)

    printf ("\nOK below is how to make $%d change!\n", dollars);
    printf ("The number of One Hundred Dollar notes is:     %d\n", OneHundredDollars);
    printf ("The number of Fifty Dollar notes is:           %d\n", FiftyDollars);
    printf ("The number of Twenty Dollr notes is:           %d\n", TwentyDollars);
    printf ("The number of Ten Dollar notes is:             %d\n", TenDollars);
    printf ("The number of Five Dollar notes is:            %d\n", FiveDollars);
    printf ("The number of Two Dollar Coins is:             %d\n", TwoDollars);
    printf ("The number of One Dollar Coins is:             %d\n", OneDollars);
    printf ("\nAND below is how to make %d cents change!\n", cents);
    printf ("The number of Fifty Cent Coins is:             %d\n", FiftyCentCoins);
    printf ("The number of Twenty Cent Coins is:            %d\n", TwentyCentCoins);
    printf ("The number of Ten Cent Coins is:               %d\n", TenCentCoins);
    printf ("The number of Five Cent Coins is:              %d\n", FiveCentCoins);


int main()
    int changes, dollars, cents;
    int a, b, c, d, e, f, g, h, i, j, k;

    /* Call Functions all the above functions */
    changes = GetAmount ();
    Convert (changes, dollars, cents);
    while (cents%5 > 0);

    DollarValues (dollars, e, f, g, h, i, j, k);
    CoinValues (cents, a, b, c, d);
    PrintChange (dollars, cents, e, f, g, h, i, j, k, a, b, c, d);

    return (0);
4 Years
Discussion Span
Last Post by WaltP

cents = (int) (((change - dollars)*100) + 0.5);

How about change this line to...

cents = (int) ((change*100.0) - (dollars*100));

and see what happen?

Edited by Taywin

/* This function is used to calculate how many of each coin is needed */
int RepeatedlyCoins (int ¢s, int coin_worth)

What is the first parameter in this function definition? Never seen a variable like that.


Hey waltP for some reason that happened in the copy haha its ment to be cents.
Thanks for the tip taywin will give it a go


Its all good people i foind it :-). In the main function i delacre the function "GetAmount" as an int where it needed to be a float. haha thats been driving me up the wall for over a week.


It wouldn't help. It's what you already have.

First things first. Fix these variables: int a, b, c, d, e, f, g, h, i, j, k;
They mean absolutely nothing and only serve to confuse anyone reading the code.

The function RepeatedlyCoins (int ¢s, int coin_worth) will return the number of coins but you never remove those coins from the the total number of cents. If you do, I don't see it.

Put some output statements in key places in the code to see if you are processing the values correctly.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.