G
G
Gimir2017-09-30 23:03:21
C++ / C#
Gimir, 2017-09-30 23:03:21

Code written in C compiles but throws an error while running?

The code written in C throws an error when I enter a number into the terminal
Here is the code itself:

#include <stdio.h>
#include <cs50.h>
int main(void) 
{
    int monets=0;
    int tf = 25;
    int t = 10;
    int f = 5;
    int o = 1;
    printf("O hai! How much change is owed?\n");
    float changeFloat = get_float();
    while (changeFloat<0) 
    {
        printf("O hai! How much change is owed?\n");
        changeFloat = get_float();
    }
    int change = changeFloat*100;
    
    while (tf<=change) {
        monets++;
    }
    while (t<=change) {
        monets++;
    }
    while (f<=change) {
        monets++;
    }
    while (o<=change) {
        monets++;
    }
    printf("%i\n", monets);
    
}

In the terminal it gives this: greedy.c
:23:15: runtime error: signed integer overflow: 2147483647 + 1 cannot be represented in type 'int'
, please tell me what's the matter?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2017-09-30
@Gimir

All your whiles run forever, you don't change tf, t, f, o, so the value of monets goes beyond the allowable signed integer type (2147483647).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question