0

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

1 Answer 1

1

It means that the value that the code is attempting to be stored in k is larger than what can be stored in this variable type.

The cause of this is an infinite loop. Lets look at that section of code.

while (r % 5 == 0)
{
    r=r-5;
    k++;
}

Knowing that the amount was 15 cents, what is happening? When the code gets to here, r is still 15 because none of the previous while tests have been true. Now, r % 5 is 0, and the loop starts. Since 5 is being subtracted from r on each pass through the loop, the remainder result from the modulo operation is ALWAYS going to be 0. Thus, it's an infinite loop. The only thing that's breaking the loop is the error generated when k exceeds the storage capacity allowed for an int.

Each while loop has the same problem. You could demonstrate this with input data divisible by 25, 10, 5, or 1.

The logic of the while loops needs a major rethink. Since that's a big part of the exercise, I'll pass on any hints at this point. ;-)

Bonus question: What's the point of i % 1 What is the modulo remainder of an integer divided by 1?

If this answers your question, please click on the check mark. Let's keep up on forum maintenance. :-)

1
  • ahh thank you! I will take another look at these while loops and figure something else out :) Commented Jan 11, 2019 at 19:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .