Cash change program using loops and if/else

前端 未结 2 1422
梦毁少年i
梦毁少年i 2021-01-17 00:55

Coding a program that allows user to input a cash amount up to $200.00 and then computes and prints its value in the following denominations (20, 10, 5, 1, .25, .10, .05, .0

相关标签:
2条回答
  • 2021-01-17 01:46

    Try this... Tested and it works. You problem was in the way reminder was being calculated. You need to divide the amt_ent again with previous step. Again all % operations work only with integers. So you need to convert your double to integer domain by multiplying with 100 before proceeding with calculations.

    #include<stdio.h>
    #include<stdlib.h>
    main()
    {
    double amt_ent1;
    int amt_ent;
    int twenty, ten, five, one, quarter, dime, nickel, penny;
    do
    { 
        printf ("Enter a dollar amount up to $200.00:");   //<== it is put in due to get the statement again else it execute with the same value.
        scanf ("%lf", &amt_ent1);
        amt_ent = (amt_ent1*100)/100;
        printf ("Name - Assignment 2 - Change-O-Matic\n");
        printf ("Amount entered: $%.2lf\n", amt_ent1);
        printf ("Change breakdown:\n");
    
        if ((amt_ent > 200.00) || (amt_ent < 00.00)) continue;
    
        amt_ent = amt_ent1 * 100;
        { /*Change in twenties*/
        twenty= (int) amt_ent/2000;
        if (twenty >= 2)
            printf("%i\t$20.00s\n", twenty);
        if (twenty == 1)
            printf ("%i\t$20.00\n", twenty);
    
            /*Change in tens*/
        amt_ent = amt_ent % 2000;
        ten = amt_ent/1000;
    
    
        if (ten >=2)
            printf ("%i\t$10.00s\n", ten);
        if (ten == 1)
            printf ("%i\t$10.00\n", ten);
    
            /*Change in fives*/
        amt_ent = amt_ent % 1000;
        five = amt_ent/500;
    
        if (five >= 2)
            printf ("%i\t$5.00s\n", five);
        if (five == 1)
            printf ("%i\t$5.00\n", five);
    
            /*Change in ones*/
        amt_ent = amt_ent % 500;
        one = amt_ent/100;
    
        if (one >= 2)
            printf ("%i\t$1.00s\n", one);
        if (one == 1)
            printf ("%i\t$1.00\n", one);
    
            /*Change in quarters*/
        amt_ent = amt_ent % 100;
        quarter = amt_ent/25;
    
        if (quarter >= 2)
            printf ("%i\t$.25s\n", quarter);
        if (quarter == 1)
            printf ("%i\t$.25\n", quarter);
    
        /*Change in dimes*/
        amt_ent = amt_ent % 25;
        dime = amt_ent/10;
    
        if (dime >= 2)
            printf ("%i\t$.10s\n", dime);
        if (dime == 1)
            printf ("%i\t$.10\n", dime);
    
            /*Change in nickels*/
        amt_ent = amt_ent % 10;
        nickel = amt_ent/5;
    
        if (nickel >= 2)
            printf ("%i\t$.05s\n", nickel);
        if (nickel == 1)
            printf ("%i\t$.05\n", nickel);
    
            /*Change in pennies*/
        amt_ent = amt_ent % 5;
        penny = amt_ent/1;
    
        if (penny >= 2)
            printf ("%i\t$.01s\n", penny);
        if (penny == 1)
            printf ("%i\t$.01\n", penny);
        }
    }
    
    while ((amt_ent <= 2000) && (amt_ent >= 0));  //<== it is put inside the do loop it is wrong, it come outside the do lopp.
    
    return 0;
    }
    
    0 讨论(0)
  • 2021-01-17 01:56

    As regarding you error, there is extra brace present after printf ("Change breakdown:\n");. And you need not to put closing brace after while statement. while ((amt_ent <= 200.00) && (amt_ent >= 00.00));} Remove that also.

    For you valid amount handling problem, there is a continue command which skips the remaining loop and reiterate from beginning when encountered. You can use that.

    do
      { 
          printf ("Enter a dollar amount up to $200.00:");
          scanf ("%lf", &amt_ent);
          if(amount_ent < 00.00 || amount_ent>200.00)
               continue;
          printf ("Name - Assignment 2 - Change-O-Matic\n");
          printf ("Amount entered: $%.2lf\n", ((amt_ent*100)/100));
          printf ("Change breakdown:\n");
    

    If amount entered is invalid, the continue statement will execute and remains loop is skipped. Then printf ("Enter a dollar amount up to $200.00:"); will be executed. So you can see, user wont be able to go ahead of continue unless he enters correct value of amount.

    0 讨论(0)
提交回复
热议问题