-4

The error returned is "Unhandled exception at 0x01355144 in Homework_3_Problem_15.exe: 0xC0000094: Integer division by zero."

I can see that the variable 'greatestCommonDivisor' is being assigned a value of 0, but I can't figure out why.

Here's my code:

#include "stdafx.h"
#include <iostream>
using namespace std;

int greatestCommonDivisor;

void inputNumbers(int& numerator, int& denominator);
int convertToGreatestCommonDivisor(int numerator, int denominator);
void convertToLowestTerms(int& numerator, int& denominator);

int _tmain(int argc, _TCHAR* argv[])
{
    int numerator = 1;
    int denominator = 1;

    inputNumbers(numerator, denominator);
    convertToGreatestCommonDivisor(numerator, denominator);
    convertToLowestTerms(numerator, denominator);

    return 0;
}

void inputNumbers(int& numerator, int& denominator)
{
    cout << "\n";
    cout << "Please enter the numerator: ";
    cin >> numerator;
    cout << "Please enter the denominator: ";
    cin >> denominator;
    cout << "\n";
    return;
}

int convertToGreatestCommonDivisor(int numerator, int denominator)
{
    int greatestCommonDivisor;
    int i;
    // checks if i is greater than the numerator *and* the denominator
    for (i = 1; i <= numerator && i <= denominator; i++)
    {
        if (numerator % i == 0 && denominator % i == 0)
        {
            greatestCommonDivisor = i;
        }
    }
    return (greatestCommonDivisor);
}

void convertToLowestTerms(int& numerator, int& denominator)
{
    cout << "The fraction " << numerator << " / " << denominator << " reduces to " <<
    (numerator / greatestCommonDivisor) << " / " << (denominator / greatestCommonDivisor);
}

Any help you can give would be greatly appreciated.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141

4 Answers4

3

You seem to misunderstand how returning from a function works.

In convertToGreatestCommonDivisor, you have a local variable called greatestCommonDivisor. You assign some values to this and then return it. When you call convertToGreatestCommonDivisor(numerator, denominator);, you are ignoring this return value.

Then, convertToLowestTerms uses the global variable greatestCommonDivisor that is declared on line 5. This variable has value 0.

It looks like you probably want to store the value returned from convertToGreatestCommonDivisor and then pass it to convertToLowestTerms:

int greatestCommonDivisor = convertToGreatestCommonDivisor(numerator, denominator);
convertToLowestTerms(greatestCommonDivisor, numerator, denominator);

This requires adding an additional parameter to convertToLowestTerms. The global variable is not necessary at all.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
1
int greatestCommonDivisor;

Is global and if uninitialized, is automatically equal to 0. I think you meant to do this:

greatestComonDivisor = convertToGreatestCommonDivisor(numerator, denominator);
yizzlez
  • 8,757
  • 4
  • 29
  • 44
1

Since greatestCommonDivisor is global, simply you can change your convertToGreatestCommonDivisor function to

void convertToGreatestCommonDivisor(int numerator, int denominator)  //Change return type to void
{
    // Remove the local variable "greatestCommonDivisor"
    int i;
    // checks if i is greater than the numerator *and* the denominator
    for (i = 1; i <= numerator && i <= denominator; i++)
    {
        if (numerator % i == 0 && denominator % i == 0)
        {
            greatestCommonDivisor = i;
        }
    }
    //Remove the return statement
}
haccks
  • 104,019
  • 25
  • 176
  • 264
  • @user3654700, I highly recommend getting rid of the global variable. – chris Jun 19 '14 at 20:41
  • @Elizabeth; I glad that it helped you :). By the way `Elizabeth` is much better than `user3654700` :D – haccks Jun 19 '14 at 20:42
  • @chris; Agreed. But when program runs successfully without changing much to it then the happiness becomes double for a newbie :) – haccks Jun 19 '14 at 20:43
  • @haccks, And then there's the skill everyone should acquire of writing a bunch of code and then later throwing it away without being attached to it. Kind of makes it seem a bit heartless when it's put like that. – chris Jun 19 '14 at 20:47
0

You have 2 greatestCommonDivisor variables one is global one is local. Global one is always 0,undefined or holds some random value depending on your environment build type etc.

Barış Uşaklı
  • 13,440
  • 7
  • 40
  • 66