C语言找零程序问题求助:换行符异常与while循环未执行
Hey there! Let's walk through troubleshooting your two C program issues step by step—no full code rewrites, just targeted clues to help you fix things yourself.
\n First, let's tackle the wonky newline behavior. The most likely culprit is your scanf call:
scanf("%lf %lf ", &price, &tendered);
Notice the trailing space at the end of the format string? In C, whitespace in scanf format strings tells it to keep reading until it hits a non-whitespace character. That means after you enter your two numbers and press enter, the program will sit waiting for more input (since enter counts as whitespace) before proceeding. This delay makes your output look messed up, like you need extra spaces or lines to get it to show up.
Troubleshooting Steps:
- Remove the trailing space from your
scanfformat string: change it toscanf("%lf %lf", &price, &tendered); - Test the program again—your
printfstatements with\nshould now display immediately as expected.
Your while loop has two critical problems that are either stopping it from running or making it useless:
Problem A: Backwards Condition Logic
Look at your first check inside the loop:
if(changeDue<=100.00){ changeDue=changeDue-100.00; hundred=hundred+1; }
Wait a second—you're checking if changeDue is less than or equal to 100 before subtracting 100? That only makes sense if you want to overdraw the change (which you don't!). You need to check if changeDue is greater than or equal to the denomination to know you can give that bill/coin. So this should be if(changeDue >= 100.00).
This backwards logic applies to all your denomination checks (20, 10, 5, 2, 1). Right now, if your change is larger than 100, none of these triggers fire, so changeDue never gets modified—and the loop either runs forever (since changeDue != 0 is always true) or seems like it's not executing at all.
Problem B: Floating Point Precision Issues
Using while(changeDue != 0.00) is a bad idea with double variables. Floating point numbers have tiny precision errors—for example, subtracting 0.25 from 0.3 might leave you with 0.04999999999999999 instead of 0.05, or subtracting pennies might never get you exactly to 0. This means your loop will either run infinitely or exit early when it shouldn't.
Troubleshooting Steps:
- Flip all your denomination conditions: Change every
<=to>=for the bill/coin checks (e.g.,changeDue >= 20.00instead of<=20.00). - Fix the floating point comparison:
- Option 1: Convert all values to integers (multiply by 100 to work in cents instead of dollars—this avoids floating point errors entirely).
- Option 2: Use a small threshold instead of checking for exact 0, like
while(changeDue > 0.001)(this accounts for tiny precision gaps).
- Scrap the unnecessary for loop: The
for(int i=0;i<changeDue;i++)block doesn't make sense here—iis an integer, so it'll never equal 0.25, 0.10, etc. Replace this with simple checks for each coin denomination (using the corrected>=logic, just like the bills).
Quick Debug Tip:
Add a printf("Current change due: %lf\n", changeDue); inside your while loop. This will show you exactly how changeDue is (or isn't) changing each iteration, which makes it way easier to spot where the logic is breaking.
内容的提问来源于stack exchange,提问作者krgarh jagjbar

