0

I have a very bizarre problem I am experiencing. When I input any number the output by my program is as such:

coins: 2

mQ + mD + mN + pN = -1055516176

When I run the program again it outputs nothing and I have to restart the terminal. Any guesses as to what's going on?

Any advice is appreciated! Thanks all.

Here is my code for PSET1 - Cash:

#include <stdio.h>

#include <cs50.h>

#include <math.h>

int main(void)

{

    float coins;

    do
    {
    coins = get_float("coins: ");
    }
    while (coins <= 0);

    int quarters = 25, dimes = 10, nickels = 5, pennies = 1;
    int dQ, mQ, dD, mD, dN, mN, dP;

    int cents = round(coins * 100);

    dQ = (cents / quarters);
    mQ = (cents % quarters);

    while (dimes <= mQ)
    {
        dD = (mQ / dimes);
        mD = (mQ % dimes);
    }
    while (nickels <= mD)
        dN = (mD / nickels);
        mN = (mD % nickels);
    }
    while (pennies <= mN)
    {
        dP = (mN / pennies);
    }
    printf("dQ + dD + dN + dP = %i\n", dQ + dD + dN + dP);
    return 0;
}
1

The "unpredictible results" is because the variables dQ,mQ etc are not initialized. With input of 2 coins, it's just using whatever happens to be in the memory of the D,N,P variables.

Program will eventually get stuck in an infinite loop. If input is .41 (41 cents), program will be stuck here while (dimes <= mQ) because neither dimes not mQ changes inside the loop; so the while (10 < 16)expression is always true.

Does the program really need while loops? Each coin amount is calculated, and the remainder is used as the "base" for the next coin. It doesn't appear to need whiles.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .