I've been working on a program for a couple of days in my beginner's C class. I've written, rewritten, broken down, and rewritten this code already, googling all of my questions with little help. I suspect what I'm doing wrong is very simple, but I think I'm just not understanding.
The program is meant to prompt a cashier for a price, an amount tendered, calculate the change, and tell the cashier how many of each bill / coin to give back. The code from void main(void) to change=round was supplied by my instructor. I understand that's how I'm supposed to turn a double number to an int. It took a long time to figure out that lf was the only format that would work for my scanf's, and my instructor specifically asked to use lots of defines.
When the program is run, it successfully prompts for price and amount paid, but change is always output to –2,147,483,648 (which I know is the lowest value for an int). I don't, however, know why it's picking that value. The math for change = round, where it calculates paid - cost, was supplied by my instructor, and I did not toy with it.
Further, my compiler is giving me the warning "_____ is used uninitialized for this function" for "twenties, tens, fives," etc listed under //Change List. I thought I had initialized them when I listed them as int's at the top? The way my instructor has described things so far has seemed a little esoteric to me, so I'm certain I'm just missing something super simple. Any explanations for the difference between int and int*, or why %f wouldn't work where I have since listed %lf would also be greatly appreciated, as they were previous headaches for me, and rewarded with bonus internet points.
Thanks in advance. Hope I formatted all right.
#define TWENTY 2000
#define TEN 1000
#define FIVE 500
#define SINGLE 100
#define QUARTER 25
#define DIME 10
#define NICKEL 5
#define PENNY 1
int round(double num)
return (int) (num + 0.5);
int change, // to be paid, in cents
twenties, tens, fives, singles, quarters, dimes, nickels, pennies;
//bills and coins used
double cost, // item cost, in dollars and cents
paid; // amount paid, in dollars and cents
change = round((paid - cost) * 100.0);
//Ask for cost
printf("Enter amount cost ($): ");
//Ask for amount paid
printf("Enter amount tendered ($): ");
printf("Change is: %d\n", change);
printf("$20 bills: %d\n", twenties);
printf("$10 bills: %d\n", tens);
printf("$5 bills: %d\n", fives);
printf("$1 bills: %d\n", singles);
printf("Quarters: %d\n", quarters);
printf("Dimes: %d\n", dimes);
printf("Nickels: %d\n", nickels);
printf("Pennies: %d\n", pennies);
twenties = (change / TWENTY);
change = (change % TWENTY);
tens = (change / TEN);
change = (change % TEN);
fives = (change / FIVE);
change = (change % FIVE);
singles = (change / SINGLE);
change = (change % SINGLE);
The main problem is that you actually do print out uninitialized variables:
// Here they are: int change, twenties, tens, fives, singles, quarters, dimes, nickels, pennies; // Then you print it out, still unitialized: printf("$20 bills: %d\n", twenties); // Then you give it a value twenties = (change / TWENTY);
Statements within a block happen in the same order that the statements occur. The assignment represents an immediate calculation; it is not setting up a formula for future (or past) application.
To fix this you will need to move all the assignment statements (
twenties = etc.) up to before the
You make the same mistake with
change. First you write:
// here "paid" and "cost" are uninitialized change = round((paid - cost) * 100.0);
and then you give them values:
// too late, horse has bolted scanf("%lf", &cost); scanf("%lf", &paid);
The amounts need to be entered before the change is calculated. Which has to be before the coins are calculated from the change.
One other thing is that the
round function should be moved to be before the start of
main(). In Standard C it's illegal to have nested function definitions.