I am very new to coding and I can't seem to understand what is causing the runtime error: signed integer overflow. My code runs and allows me to enter a change amount when prompted, but fails after that.
I understand that an overflow error means I've run out of memory because of a very large number, but I don't understand why this code is generating such a large number. I tried googling this error to see if I could troubleshoot this myself, but I have been unable to find any relevant solutions so far. I'm aware that I may have multiple problems here and I would really appreciate any help that may start me in the right direction. Thank you!
Below is my code:
#include <cs50.h>
#include <stdio.h>
#include <math.h>
int main (void)
{
float c;
// asks how much change is owed until nonnegative answer;
do
{
c = get_float("How much changed is owed? ");
}
while (c < 0);
// round c to be only cents, stored as integer instead of float;
int r = round(c*100);
// time for the coins (k = coins);
int k = 0;
while (r % 25 == 0)
{
r=r-25;
k++;
}
while (r % 10 == 0)
{
r=r-10;
k++;
}
while (r % 5 == 0)
{
r=r-5;
k++;
}
while (r % 1 == 0)
{
r=r-1;
k++;
}
printf("%i\n", k);
}
Here is an example of the error:
$ make cash
clang -fsanitize=signed-integer-overflow -fsanitize=undefined -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow cash.c -lcrypt -lcs50 -lm -o cash
$ ./cash
How much changed is owed? .15
cash.c:31:12: runtime error: signed integer overflow: -2147483645 - 5 cannot be represented in type 'int'
^C