Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
#include <stdio.h>

void print_line(int index, int cents, int value)
{
    float count = (float)cents / (float)value;
    float dollar_part = (float)value / 100;
    float cents_part = (float) value % 100;
    printf("| %-2d | %9f.%02f | %5f |\n", index, dollar_part, cents_part, count);
}

void coins(int cents)
{
    printf("You did not type in the correct format in terms of dollars and cents.\n");
    printf("\n+----+--------------+-------+\n");
    printf("| #  | Denomination | Count |\n");
    printf("+----+--------------+-------+\n");
    print_line(1, cents, 10000);
    cents = cents % 10000;
    print_line(2, cents, 5000);
    cents = cents % 5000;
    print_line(3, cents, 1000);
    cents = cents % 1000;
    print_line(4, cents, 500);
    cents = cents % 500;
    print_line(5, cents, 200);
    cents = cents % 200;
    print_line(6, cents, 100);
    cents = cents % 100;
    print_line(7,  cents, 50);
    cents = cents % 50;
    print_line(8, cents, 20);
    cents = cents % 20;
    print_line(9, cents, 10);
    cents = cents % 10;
    print_line(10,cents, 5);
    cents = cents % 5;
    print_line(11, cents, 1);
    cents = cents % 1;

    printf("+----+--------------+-------+\n");

    printf("\n+----+--------------+-------+\n");
    printf("| #  | Denomination | Count |\n");
    printf("+----+--------------+-------+\n");
    print_line(1, cents, 10000);
    cents = cents % 10000;
    print_line(2, cents, 5000);
    cents = cents % 5000;
    print_line(3, cents, 1000);
    cents = cents % 1000;
    print_line(4, cents, 500);
    cents = cents % 500;
    print_line(5, cents, 200);
    cents = cents % 200;
    print_line(6, cents, 100);
    cents = cents % 100;
    print_line(7,  cents, 50);
    cents = cents % 50;
    print_line(8, cents, 20);
    cents = cents % 20;
    print_line(9, cents, 10);
    cents = cents % 10;
    print_line(10,cents, 5);
    cents = cents % 5;
    print_line(11, cents, 1);
    cents = cents % 1;

    printf("+----+--------------+-------+\n");

    printf("\n+----+--------------+-------+\n");
    printf("| #  | Denomination | Count |\n");
    printf("+----+--------------+-------+\n");
    print_line(1, cents, 10000);
    cents = cents % 10000;
    print_line(2, cents, 5000);
    cents = cents % 5000;
    print_line(3, cents, 1000);
    cents = cents % 1000;
    print_line(4, cents, 500);
    cents = cents % 500;
    print_line(5, cents, 200);
    cents = cents % 200;
    print_line(6, cents, 100);
    cents = cents % 100;
    print_line(7,  cents, 50);
    cents = cents % 50;
    print_line(8, cents, 20);
    cents = cents % 20;
    print_line(9, cents, 10);
    cents = cents % 10;
    print_line(10,cents, 5);
    cents = cents % 5;
    print_line(11, cents, 1);
    cents = cents % 1;

    printf("+----+--------------+-------+\n");

}

int main(void)
{
    int dollars, cents;
    printf("Please enter total value: ");
    int n = scanf("%d.%d", &dollars, ¢s);
    if (n != 2)
    {
        printf("You did not type in the correct format in terms of dollars and cents.\n");
    }
    else if (dollars < 0)
    {
        printf("You did not type in the correct format in terms of dollars and cents.\n");
    }
    else if (cents < 0 || cents > 99)
    {
        printf("You did not type in the correct format in terms of dollars and cents.");
    }
    else
    {
        int total_cents = dollars * 100 + cents;
        coins(total_cents);
    }
return 0;


    printf("main()\n");
}


My code looks like this. For line
float cents_part = (float)value % 100;
it gives an error
invalid operands to binary % (have ‘float’ and ‘int’)
. How do I fix this?

What I have tried:

Honestly I'm quite lost, still new to this for school.
Posted
Updated 1-Oct-21 21:37pm
Comments
Richard MacCutchan 2-Oct-21 4:09am    
Don't use float types for financial values. Stick to integers, and make sure all values are numbers of cents. It is then a simple matter to separate the number of dollars without ending up with odd numbers of cents.
jeron1 2-Oct-21 11:30am    
Wise words.
Richard MacCutchan 2-Oct-21 11:41am    
:)

You are close. Try this :
C++
void print_line(int index, int cents, int value)
{
    double count = (double)cents / (double)value;
    int dollar_part = value / 100;
    int cents_part = value % 100;
    printf("| %-2d | %9d.%02d | %6.1f |\n", index, dollar_part, cents_part, count);
}
One can make a value a float with the .0f suffix. I also adjusted the printf format string to have a .0 in a few fields so no decimal places appear.

ETA: I noticed this received a one-vote and I was thinking why would that be? I looked closer at the code and said out-loud, "DUH!" One can not use the modulus operator with floating point values. This solution has been modified to be correct. The fact is NONE of those need to be floating values except possibly the first one but with the values passed it's not necessary.

Also - you have the same sequence of code repeated several times in your question. If that is really what you have then you should consider making a function out of that piece of code.
 
Share this answer
 
v3
Merely examine the language requirements for the modulo operator as per e.g. cppreference.com
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900