Why won't my C program work properly?

Discussion in 'OT Technology' started by vibrantglow, Sep 21, 2003.

  1. vibrantglow

    vibrantglow Guest

    It takes a dollar/cents amount from the user and translates it into the minimal amount of coins (hd, q, d, n, p).

    It seems to work okay, until it gets to the penny section.

    Any tips?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
    double change_amt, cents_amt;
    int half_dollar, quarter, dime, nickel, penny, hd, q, d, n, p;
    half_dollar = 50;
    quarter = 25;
    dime = 10;
    nickel = 5;
    penny = 1;
    printf("What is the amount you would like in coins? ");
    scanf("%lf", &change_amt);
    cents_amt = change_amt * 100;
    hd = cents_amt / half_dollar;
    printf("$%2.2f would contain:\n", change_amt);
    printf("%2.0d half-dollar(s)\n", hd);
    cents_amt = cents_amt - (hd * half_dollar);
    q = cents_amt / quarter;
    printf("%2.0d quarter(s)\n", q);
    cents_amt = cents_amt - (q * quarter);
    d = cents_amt / dime;
    printf("%2.0d dime(s)\n", d);
    cents_amt = cents_amt - (d * dime);
    n = cents_amt / nickel;
    printf("%2.0d nickel(s)\n", n);
    cents_amt = cents_amt - (n - nickel);
    p = cents_amt / penny;
    printf("%2.0d penny(ies)\n", p);
    system("pause");
    }
    
     
  2. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    Code:
    cents_amt = cents_amt - (n - nickel);
    Should be * not -. Also, you don't really need to do cents_amt / penny, since it just ends up as cents_amt anyway.
     

Share This Page