C语言找零程序显示金额后终止运行求助(Java转C新手)
Hey there! Let's figure out why your change-making program is freezing up after displaying the tendered amount. As someone who's jumped between Java and C before, I totally get how easy it is to hit language-specific quirks. Let's break down each version's issues and fix them step by step:
Key Issues in Each Version
Version 1
- Floating-Point Precision Disaster
Usingdoublefor currency is a huge red flag. Binary floats can't represent decimal values like 0.1 exactly, so yourwhile(changeDue != 0.00)loop will never exit—changeDuewill never be perfectly 0. This is why your program hangs. - Backwards Condition Logic
Yourif(changeDue <= 100.00)check is reversed! You want to subtract a bill/coin only ifchangeDueis greater than or equal to its value. For example, ifchangeDueis 50, subtracting 100 would make it negative, leading to endless incorrect calculations. - Useless For Loop
The loopfor(int i=0;i<changeDue;i++)makes no sense here:iis an integer, so ifchangeDueis less than 1 (like 0.75), the loop won't run at all. Plus, checkingi == 0.25is impossible—iis an int, it can never equal a decimal.
Version 2
- Type Mismatch in Input
You declaredpriceandtenderedasdouble, but usedscanf("%d", &price)—%dis for integers, not doubles. This corrupts memory and causes undefined behavior (which can lead to crashes or hangs). - Incorrect Output
printf("%d", &tendered);passes the address oftenderedinstead of its value. Printing an address with%dis invalid and will output garbage, plus this contributes to the program's unstable state.
Version 3
- Same Floating-Point Problem
Switching tofloatdoesn't fix the precision issue—you'll still get an infinite loop when checkingchangeDue != 0. - Wrong Types for Coin Counts
Variables liketoonoeandquartercount the number of coins/bills, which are integers. Declaring them asfloatis unnecessary and can lead to weird rounding errors.
Fixed Version (Using Integer Currency for Precision)
The standard fix for currency in C is to work in cents (use integers) instead of dollars. This eliminates all floating-point precision issues. Here's a corrected implementation:
#include <stdio.h> int main(void) { int priceCents; // Total price in cents int tenderedCents; // Amount received in cents int changeDue; // Bill/coin counts (all integers) int hundred = 0, twenty = 0, ten = 0, five = 0; int toonie = 0, loonie = 0; int quarter = 0, dime = 0, nickel = 0, penny = 0; printf("Enter total purchase price (dollars.cents): "); // Read as dollars and cents, convert to total cents int dollars, cents; scanf("%d.%d", &dollars, ¢s); priceCents = dollars * 100 + cents; printf("Enter amount received (dollars.cents): "); scanf("%d.%d", &dollars, ¢s); tenderedCents = dollars * 100 + cents; changeDue = tenderedCents - priceCents; if (changeDue < 0) { printf("Insufficient amount received!\n"); return 1; } // Calculate bills hundred = changeDue / 10000; // $100 = 10000 cents changeDue %= 10000; twenty = changeDue / 2000; // $20 = 2000 cents changeDue %= 2000; ten = changeDue / 1000; changeDue %= 1000; five = changeDue / 500; changeDue %= 500; // Calculate coins (CAD) toonie = changeDue / 200; // $2 = 200 cents changeDue %= 200; loonie = changeDue / 100; changeDue %= 100; quarter = changeDue / 25; changeDue %= 25; dime = changeDue / 10; changeDue %= 10; nickel = changeDue / 5; changeDue %= 5; penny = changeDue; // Print change breakdown printf("\nChange Breakdown:\n"); if (hundred > 0) printf("%d x $100 bills\n", hundred); if (twenty > 0) printf("%d x $20 bills\n", twenty); if (ten > 0) printf("%d x $10 bills\n", ten); if (five > 0) printf("%d x $5 bills\n", five); if (toonie > 0) printf("%d x $2 toonies\n", toonie); if (loonie > 0) printf("%d x $1 loonies\n", loonie); if (quarter > 0) printf("%d x 25¢ quarters\n", quarter); if (dime > 0) printf("%d x 10¢ dimes\n", dime); if (nickel > 0) printf("%d x 5¢ nickels\n", nickel); if (penny > 0) printf("%d x 1¢ pennies\n", penny); return 0; }
Why This Works:
- Using integers for cents avoids all floating-point precision errors.
- We use division and modulus to count each bill/coin efficiently (no messy loops).
- Input is read as dollars and cents to make it user-friendly, then converted to total cents.
内容的提问来源于stack exchange,提问作者gotta assk
相关产品推荐
相关产品推荐

