Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.
I think this was already due, so hopefully this isn't ruining it, but i think something like this might be better:
Code:
#include <stdio.h>
#include <math.h>

int main(int argc, char *argv[]) {
   int dollars_paid, cents_paid,dollars_due,cents_due,change,dollar_bills=0,quarters=0,dimes=0,nickels=0,owe;
   printf("Welcome.\nEnter the amounts due and paid:\nDue: $");
   scanf("%d.%d",&dollars_due,&cents_due);
   printf("Paid: $");
   scanf("%d.%d",&dollars_paid,&cents_paid);

   cents_paid+=dollars_paid*100;
   cents_due+=dollars_due*100;
   owe = cents_due > cents_paid;

   change = abs(cents_paid - cents_due);
   if(change == 0) {
     printf("Exact change! Thanks!\n");
     return 0;
   }

   dollar_bills = change / 100;
   change%=100;

   quarters = change / 25;
   change%=25;

   dimes = change / 10;
   change%=10;

   nickels = nickels / 5;
   change%=5;

   if(owe) {
     printf("You still owe ");
   } else {
     printf("Your change is ");
   }
   
   if(dollar_bills) printf("%d dollar bill(s) ",dollar_bills);
   if(quarters) printf("%d quarter(s) ",quarters);
   if(dimes) printf("%d dime(s) ",dimes);
   if(nickels) printf("%d nickel(s) ",nickels);
   if(change) printf("%d penn(y|ies)",change);
   printf("\n");
   return 0;
}

No worrying with estimation of a float or double, etc.

-Lee
 
Code:
   nickels = nickels / 5;

That's a bug. It should be:
Code:
   nickels = change / 5;

Trial run after fixing:
Code:
Welcome.
Enter the amounts due and paid:
Due: $3.1
Paid: $5.5
Your change is 2 dollar bill(s) 4 penn(y|ies)
The "4 pennies" part of the answer is wrong.

A pair of trial runs:
Code:
Welcome.
Enter the amounts due and paid:
Due: $3.25
Paid: $5
Your change is 1 dollar bill(s) 3 quarter(s) 1 penn(y|ies)

Welcome.
Enter the amounts due and paid:
Due: $3.25
Paid: $5.00
Your change is 1 dollar bill(s) 3 quarter(s)

Another trial run:
Code:
Welcome.
Enter the amounts due and paid:
Due: $3.25
Paid: $5.


[I][does not proceed until a non-whitespace char is entered][/I]

Foolproof parsing of numbers is harder than it seems, even for experienced programmers, because fools are so ingenious. Overall, I think parsing it as a float or double and converting that to cents is simpler for the students at this level.
 
I'll throw this one out there. I wrote it in about 30 minutes following the first request for assistance and didn't do but the most rudimentary of testing.

While more complicated it does allow reporting additional denominations by extending the entries in an array.

Code:
// <https://forums.macrumors.com/posts/9468838/>

// MARK: Headers

#include <math.h>
#include <stdio.h>
#include <stdlib.h>


// MARK: -
// MARK: Type Definations

enum { false, true };

typedef int bool_t;

// associates denomination name with value in pennies

typedef struct currency_t
{
    char*       name_denom;     // denomination name
    int         value_denom;    // denomination value in pennies
} currency_t;


// MARK: -
// MARK: Global Declarations

// array of denominations, order smallest value to greatest value
// insert denominations as required

const currency_t denoms_array[] = {
      { "cent",                     1 }
    , { "nickel",                   5 }
    , { "dime",                    10 }
    , { "quarter",                 25 }
//  , { "half-dollar",             50 }
    , { "dollar bill",            100 }
//  , { "two bill",               200 }
    , { "five dollar bill",       500 }
    , { "ten dollar bill",       1000 }
    , { "twenty dollar bill",    2000 }
//  , { "fifty dollar bill",     5000 }
//  , { "hundred dollar bill",  10000 }
};


// MARK: -
// MARK: Global Constants

// count of elements in array 'denoms_array'

const int   num_demons = (sizeof(denoms_array) / sizeof(denoms_array[0]));


// MARK: -
// MARK: Subroutines

// returns the count of demoninations that can be  exchanged given the count of
// pennies located at 'num_pennies' for demoninations of value 'denom_value'

int exchange_at_rate(int* num_pennies, int value_denom)
{
    int num_denoms;

    num_denoms      = (*num_pennies / value_denom);
    *num_pennies    %= value_denom;

    return num_denoms;
}


// MARK: -
// MARK: Porgram Entry Point

int main(int argc, const char* argv[])
{
    printf("Welcome.\n");

    // request purchase price ...
    double  due;
    printf("Enter the amount due: ");
    scanf("%lf", &due);

    // ... buyer tendered amount ...
    double  paid;
    while ( true )
    {
        printf("Enter the amount paid: ");
        scanf("%lf", &paid);

        if ( paid < due )
        {
            printf("The amount paid must be more than the amount due. \n");
            continue;
        }

        break;
    }

    // ... determine change due, in pennies ...
    int     change = (int)(paid * 100) - (int)(due * 100);

    if ( !change )
    {
        printf("Exact change!\n");
        return 0;
    }

    // ... report the change due ...
    printf("\nYour change is as follows: ");
    
    // ... starting with denominations of greatest value to least
    int num_of_pennies = change;
    if ( num_of_pennies )
    {
        bool_t      enabled_and = false;
        
        // starts at end of array with denominations of greatest value
        int index_denoms_array = num_demons - 1;
        do {
            const char* name_denom  = denoms_array[index_denoms_array].name_denom;
            int value_denom         = denoms_array[index_denoms_array].value_denom;
            
            int num_denom           = exchange_at_rate(&num_of_pennies, value_denom);
            if ( num_denom )
            {
                if ( enabled_and )
                {
                    printf("%s", (!num_of_pennies ) ? " and " : ", ");
                }
                
                enabled_and = true;
                
                // output denomination count and name ...
                printf("%d %s", num_denom, name_denom);
                printf("%s", (num_denom > 1) ? "s" : "");
            }
        } while ( index_denoms_array-- );
        
        printf("!\n");
    }
    
    return 0;
}
 
My apologies again.

With your first post code beginning with

Code:
#include <stdio.h>
#include <math.h>
#include <iostream>
using namespace std;

and the use of 'static_cast<int>' I made the assumption that you were learning to program around C++.

Might I suggest that in the future that you start you topics with some summary of the above information. It'll help determine how we can best assist you.

No problem -- I took out the '<iostream>' and 'static_cast<int>' once it was explained to me. Some of the issues in my learning this is getting the two straight, like learning Spanish and Italian at once - similar but different.

Lee and Chown, thank for your the further examples. I'll study them and mess around a bit. It's nice to learn using something I can mess with and edit myself, instead of pencil scribbles on looseleaf handouts. :)

Lloyd, again - the arrays that we're just finishing up will definitely help me understand further how this example works. Like I said above, studying someone's superior work is really helpful. I learn a lot better from doing than I do just seeing / listening.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.