1

I was supposed to get user input, a float, then keep track how many times 0.25, 0.10, 0.05, and 0.01 can be subtracted from it using a counter. Then it's supposed to print the number of counts. But when I tried running the code, it gets user input, but when I try any number this shows up:

greedy.c:18:14: runtime error: signed integer overflow: 2147483647 + 1 cannot be >represented in type 'int'

Please point out any errors, here is my code:

#include <stdio.h>
#include <cs50.h>
#include <math.h>

int main(void)

{

    float b;
    float a;
    int count = 0;
    printf("How much change is owed? ");
    a = GetFloat();

    do
    {
        b = a - 0.25;
        count++;
    }
    while(a>0.25);

    do 
    {
        b = a - 0.10;
        count++;
    }
    while(a>0.10);

    do 
    {
        b= a - 0.05;
        count++;
    }
    while(a>0.05);

    do
    {
        b= a- 0.01;
        count++;
    }
    while(a>0.01);
    printf("%d coins\n", count);
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 2
    `while(a>0.25);` how is the loop supposed to end when `a` never changes? – tkausl Oct 30 '16 at 15:02
  • 1
    The message is clear, what is your **specific** question? – too honest for this site Oct 30 '16 at 15:02
  • @ Olaf my code compiles but it doesn't work. When I input data it isn't able to output what I want. – user7091717 Oct 30 '16 at 15:10
  • @ tkausl a should change since it is being subtracted 0.25 – user7091717 Oct 30 '16 at 15:11
  • Note that repeated subtraction of floating point values is not good for accuracy. It is often better to convert the value (carefully — rounding?) to an integer type and work with the integer type. There's also division that is a form of repeated subtraction. – Jonathan Leffler Oct 30 '16 at 15:11
  • 1
    No; you don't change the value stored in `a`. You assign to `b` the value of `a - 0.25` in the first loop, but you'd need to add `b = a;` to change `a` as well. Though, since you don't use `b`, it would be more sensible to use `a = a - 0.25;` or even `a -= 0.25;` to change `a`. – Jonathan Leffler Oct 30 '16 at 15:13

2 Answers2

2

Instead of "b = a - 0.25;" try "a = a - 0.25;". And the same for the other subtractions. The variable a stays the same now in your loop.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user189695
  • 56
  • 3
  • I tried that just now, it gave me an actual numerical output instead of the error message above. BUT there's something wrong with the calculations I think. I tried 10 as input but what it calculated was 43. That got me thinking, why won't it just divide evenly by 0.25? – user7091717 Oct 30 '16 at 15:15
  • @jjjjj please see [this question](http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency) about "change" problems, and [this one](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) about floating point calculations.. – Weather Vane Oct 30 '16 at 15:29
  • That is technically correct, you get 40 from the first loop and you always loop at least once through the other loops so you get 3 added. Changing the while loop from `do { ... } while (...)` to `while (...) { ... }` give the result you want. But i highly recommend you start a debugger and step trough the code line by line so it becomes a lot more clear what is happening. – user189695 Oct 30 '16 at 15:34
1

You never actually change the value of a so none of your while conditions ever reach false. To fix this change every b = a - coinvalue to a = a - coinvalue (which also means you can eliminate b.

theKunz
  • 444
  • 4
  • 12
  • Thanks, I changed b to a and everything seems fine there is no error message and the output is an actual number but there's something wrong with the calculations since I tried to input 10 but the output is 43 instead of just 40, you know, just divide 10 by 0.25? The aim of this code is to subtract first the largest amount possible (0.25) from the input – user7091717 Oct 30 '16 at 15:21
  • @jjjjj So two things: 1. Change your `do { } while` to normal `while`. `do-while` loops are used when you want to iterate at least once. That way you don't end up subtracting value and increasing count when the value is already less than the coin amount 2. The reason it's 43 is that once your 0.25 coins are done and count reaches 40 it sill goes through the other coins at least once. My fix mentioned in my last point should fix this as well. – theKunz Oct 30 '16 at 16:29
  • changing the do while loop fixed everything. Thanks! – user7091717 Oct 30 '16 at 16:51
  • hey @theKunz when I input 4.2 it should give me 18 but it says 22. Help what do I do – user7091717 Oct 30 '16 at 17:04