C Unhandled exception 0xC0000094: Integer division by zero

1

I have been given this error for two days straight, and I have worked on it for about three hours, altogether. When I run it, it will tell me how many $10 bills I need but just stops at the $5 line and gives me this exception every time.

#define _CRT_SECURE_NO_WARNINGS 'code'
#include <stdio.h>]
#include <stdlib.h>
#include "math.h"

int main()
{
    int ten = 10;
    int five = 5;
    int one = 1;
    float quarter = .25;
    float dime = .10;
    float nickel = .05;
    float penny = .01;
    double money=0;

    printf("Please enter a monetary amount:");

    scanf_s("%lf", &money);//scanning the entry in, and & is allowing it to be entered

    //money is the input number

    printf("You entered: %lf \n", money);//creates too many zeroes but for now move on

    ten = money / ten;//dividing the change = 10, and then

    money = (int) money % ten;//this determines how many tens there are in the mix
    //casted it because it only needs to be an integer not a double

    printf("$10     : %d \n", ten);//printing an integer of how many tens are needed to make up money

    five = money / five;//dividing the change = 10, and then

    money = (int) money % five;//this determines how many tens there are in the mix
    //casted it because it only needs to be an integer not a double

    printf("$5      : %d \n", five);

    //first problemm I had was putting in const, and that gave me a bajillion errors


    return(EXIT_SUCCESS);
}
c
asked on Stack Overflow Apr 9, 2020 by zebratiger101

2 Answers

1

The error that you have mentioned will be shown if you divide a number by zero.just like hanie mentioned in her answer but in your case i don't see a line were you are dividing a number by zero but you have some lines were you overwrite the ten and five variables using modulo operator i guess it might be throwing the error.

I am posting a modified version of your code. Hope it helps you.

#define _CRT_SECURE_NO_WARNINGS 'code'
#include <stdio.h>
#include <stdlib.h>
#include "math.h"

int main()
{
    int tens = 0;
    int fives = 0;
    double money=0;

    printf("Please enter a monetary amount:");

    scanf("%lf", &money);
    printf("You entered: %lf \n", money);

    tens = money / 10;
    printf("$10     : %d \n", tens);

    money = money - tens * 10;

    fives = money / 5;
    printf("$5      : %d \n", fives);

    money = money - fives * 5;

    return(EXIT_SUCCESS);
}

Sample Input and Output

Please enter a monetary amount:125
You entered: 125.000000
$10     : 12
$5      : 1

Please enter a monetary amount:123
$10     : 12
$5      : 0

answered on Stack Overflow Apr 9, 2020 by Mohamed Shabeer kp • edited Apr 9, 2020 by Mohamed Shabeer kp
0

Note that most implementations of floating point math will follow a standard (e.g. IEEE 754), in which case operations like divide-by-zero will have consistent results and the C standard says the operation is undefined.

before doing division you need to do something like this:

if(denominator != 0) 
a = a / denominator;

or

if(denominator != 0) 
a = a % denominator;
answered on Stack Overflow Apr 9, 2020 by hanie

User contributions licensed under CC BY-SA 3.0