数组的划分和C的双重划分

时间:2015-03-01 22:28:07

标签: c arrays loops double division

所以我有一个创建变革制造者的任务。我差点把它弄下来,我只有一个问题。一切顺利,直到它划分循环的最后一次迭代。如果我使用55.87作为值,一旦循环到达最后一个.02并将其除以.01(希望得到2),它将获得1。这是我的代码:

#include <stdio.h>

int main (void)
{
    double money;
    double change[8] = {20, 10, 5, 1, .25, .10, .05, .01};
    int quantity;
    int i;
    int rpt = 1;

    printf("Kevin Welch - Assignment 2 - Change Maker");

    while (rpt == 1)
    {
        printf("Enter a dollar amount up to 200 to make change.\n");
        printf("Maximum of two decimal points for cents.\n");
        scanf("%lf", &money);

        while (money > 200 || money < 0)
        {
            printf("Not a valid value.\nPlease re-enter:\n");
            scanf("%lf", &money);
        }

        printf("\nAmount entered: $%.2lf\n\n", money);
        printf("Change breakdown:\n");

        for (i = 0; i < 8; i++)
        {
            quantity = (money / change[i]);

/*
 I added this next printf line to output my variables to see what they        
 were doing while in the loop. 
 Output shows money = 0.02 and change[i] = 0.01, 
 however (money/change[i]) = 1 instead of 2.
*/

            printf("\n%.2lf  %i   %.2lf\n\n", money, quantity, change[i]);

            if (quantity >= 2 && money != 0)
            {
                if (change[i] >= 10)
                    printf("%i $%.2lfs\n", quantity, change[i]);
                else
                    printf("%i  $%.2lfs\n", quantity, change[i]);
                money -= quantity * change[i];
            }
            else if (quantity >= 1 && money != 0)
            {
                if (change[i] >= 10)
                    printf("%i $%.2lf\n", quantity, change[i]);
                else
                    printf("%i  $%.2lf\n", quantity, change[i]);
                money -= quantity * change[i];
            }
        }
        printf("\nWould you like to use the change maker again?\n");
        printf("Enter 0 for No and 1 for Yes:\n");
        scanf("%i", &rpt);
        while (rpt != 0 && rpt !=1)
        {
            printf("Enter 0 for No and 1 for Yes\n");
            scanf("%i", &rpt);
        }
    }
}

0 个答案:

没有答案