looking advice why running strangely. on .25
works perfectly, lower .25
, i'm getting strange results. what's wrong?
#include <cs50.h> #include <stdio.h> #include <math.h> int main(void) { int v; int count; int q= 25; int d= 10; int n= 5; int p= 1; float m; { printf("how many coins need change?:\n"); m=getfloat(); } while (m<0); int balance; m=m*100; balance= round (m); while (balance >= q) { v=balance/q; count=v; balance=balance-(v*q); } while (balance >= d) { v=balance/d; count=count+v; balance=balance-(v*d); } while (balance >= n) { v=balance/n; count=count+v; balance=balance-(v*n); printf("%i\n", count); printf("%i\n", count); } while (balance >= p) { v=balance/p; count=count+v; balance=balance-(v*p); } printf("total number of coins needed:%i\n", count); }
for values lower 0.25, never go first while section (since balance < q), , therefore count remains uninitialized, leading undefined results.
just change -
int count;
into
int count = 0;