-2

I want to solve a question like this:

Enter a specified amount (in units of yuan, such as 345.78) from the keyboard, and then display the amount of RMB in various denominations that pay the amount, and ask to display 100 yuan, 50 yuan, 10 yuan, 5 yuan, 2 yuan, 1 yuan, 5 angles, 1 point, 5 points, 1 point each. like 345.78=100*3+10*4+5*1+0.5*1+0.1*2+0.01*8

Here are the codes:

#include<stdio.h>
int main()
{int a1=0,a2=0,a3=0,a4=0,a5=0,a6=0,a7=0,a8=0,a9=0; 
float m;
printf("enter money:");
scanf("%f",&m);
while(m>100.0)
{m=m-100;
a1++;}
while(m>50.0)
{m=m-50;
a2++;}
while(m>10.0)
{m=m-10;
a3++;}
while(m>5.0)
{m=m-5;
a4++;}
while(m>2.0)
{m=m-2;
a5++;}
while(m>1.0)
{m=m-1;
a6++;}
while(m>0.1)
{m=m-0.1;
a7++;}
while(m>0.05)
{m=m-0.05;
a8++;}
while(m>0.01)
{m=m-0.01;
a9++;}
printf("a1=%d,a2=%d,a3=%d,a4=%d,a5=%d,a6=%d,a7=%d,a8=%d,a9=%d\n",a1,a2,a3,a4,a5,a6,a7,a8,a9);
return 0;
}

#include<stdio.h>

int main()
{
    double n,r[8];
    int k;
    scanf("n=%lf",&n);
    int a=n/100;
    r[0]=n-a*100;
    int b=r[0]/50;
    r[1]=r[0]-b*50;
    int c=r[1]/10;
    r[2]=r[1]-c*10;
    int d=r[2]/5;
    r[3]=r[2]-d*5;
    int e=r[3]/2;
    r[4]=r[3]-e*2;
    int f=r[4];
    r[5]=r[4]-f*1;
    int g=r[5]*10.00;
    r[6]=r[5]-g*0.1;
    int h=r[6]*20.00;
    r[7]=r[6]-h*0.05;
    int i=r[7]*100.00;
    printf("%d\n",a);
    printf("%d\n",b);
    printf("%d\n",c);
    printf("%d\n",d);
    printf("%d\n",e);
    printf("%d\n",f);
    printf("%d\n",g);
    printf("%d\n",h);
    printf("%d\n",i);
    return 0;
}

However, while entering the numbers like 0.78, 0.98, 0.99, the value i is ALWAYS one less.

How does this happen?

Kevin
  • 16,549
  • 8
  • 60
  • 74
Normed
  • 27
  • 5
  • 2
    Welcome to Stack Overflow! It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and [Debugging Guide](http://idownvotedbecau.se/nodebugging/) – NathanOliver Nov 19 '19 at 13:39
  • 4
    You have TWO `main` functions. The lack of indentation makes it very hard to read your code. Trying to compile this code gives me about a dozen warnings. – Blaze Nov 19 '19 at 13:41
  • This is a problem not suited for floating point arithmetic (see [here](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) for details). Parse the input into two integers. – molbdnilo Nov 19 '19 at 13:44
  • I'm using the VS2019 and here is the warning information:Warning C26451: Arithmetic overflow: Use the operator "% operator%" for the % byte value for % name> and then convert the result to a % z e 1% byte value. Cast values to a larger type before calling the operator "% operator%" to avoid overflow – Normed Nov 19 '19 at 13:51
  • 1
    Use integers, not floating point. Things like this: `while(m>0.1) {m=m-0.1` are not guaranteed to run the specific number of times you think it will run. The floating point number `0.1` cannot be represented exactly as binary, and subtracting `0.1` from it leads to produce more rounding errors. Multiply *everything* by 100, do the math in integer, and *at the end*, divide back by 100 if necessary. – PaulMcKenzie Nov 19 '19 at 13:59

1 Answers1

1

You should avoid floating point arithmetic. Read the input as floating point and store the number of smallest units as integer:

#include <iostream>

int main()
{
    double input;
    std::cin >> input;
    int money = input * 100;
    for (auto n : {10000, 5000, 1000, 500, 200, 100, 10, 5, 1}) {
        std::cout << n / 100.0 << ":\t" << money / n << '\n';
        money %= n;
    }
    return 0;
}

Input:

1288.56

Output:

100:    12
50:     1
10:     3
5:      1
2:      1
1:      1
0.1:    5
0.05:   1
0.01:   1
Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62