c - Loop runs multiple times after input before allowing input -
so whenever run code, code runs , prints out results, runs again , prints arbitirary results, , again, , again, lets me input loop. not find error , tried searching online.
help appreciated
while (x > 0) { printf("please enter money , nickels (ex. $12.63 , 3 nickels). \nenter 0 end program:\n"); scanf("%c %lf %s %d", &dollar, &money, &andword, &nickel_input); if (dollar == '0') { printf("thanks using program!"); return 0; } money *= 100; money -= nickels * 5; quarters = (money / 25); money -= quarters * 25; dimes = (money / 10); money -= dimes * 10; nickels = (money / 5); money -= nickels * 5; pennies = (money / 1); money -= pennies * 1; coins = quarters + dimes + nickels + pennies; nickels += nickel_input; printf("the fewest number of coins have %d:\n", coins); printf("# quarters: %d\n", quarters); printf("# dimes: %d\n", dimes); printf("# nickels: %d\n", nickels); printf("# pennies: %d\n\n", pennies); }
by way, excerpt code if didn't know.
the best way avoid situations scanf
leaves trash on input read line fgets
, parse line sscanf
. sure check return value both functions. fgets
return null if end-of-file reached. sscanf
returns number of successful conversions, should 4 in case.
Comments
Post a Comment