Help with a huge integer problem

This is a discussion on Help with a huge integer problem within the C++ Programming forums, part of the General Programming Boards category; Basically, this problem is supposed to create a class HugeInteger that uses a 40-element array of digits to store integers ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    2

    Help with a huge integer problem

    Basically, this problem is supposed to create a class HugeInteger that uses a 40-element array of digits to store integers as large as 40 digits each. It's also supposed to provide member functions input, output, add, subtract, multiply, divide, and modulus. Also, for comparing HugeInteger objects, it's supposed to provide predicate functions (true or false) isEqualTo, isNotEqualTo, isGreaterThan, isLessThan, isGreaterThanOrEqualTo, isLessThanOrEqualTo, and isZero. When I ran the program, an error occurs at the multiplication part. The program executes the add and subtract functions perfectly, but then I get this error message on the multiplication part:

    Code:
    Run-Time check failure #2 - Stack around the variable 'c' was corrupted.
    The error was cited in the last line of my header implementation file (the second set of code below). Here's what I have so far:

    Code:
    // definition of HugeInteger class
    
    #ifndef HugeInteger_H
    #define HugeInteger_H
    
    class HugeInteger
    {
    private:
         char cTheNumber[41];
         bool bIsError;
         void initializeTheNumber();
    public:
         HugeInteger();
         int theLength();
        void inputHugeInteger(); 
         void outputHugeInteger();
         void addHugeIntegers (HugeInteger,HugeInteger);
         void subtractHugeIntegers (HugeInteger,HugeInteger);
         void multiplyHugeIntegers (HugeInteger,HugeInteger);
         HugeInteger multiplySingleDigit (HugeInteger a, int theDigit, int thePosition);
         HugeInteger divideHugeIntegers (HugeInteger,HugeInteger);
         HugeInteger modulusHugeIntegers (HugeInteger,HugeInteger);
         bool isEqualTo (HugeInteger,HugeInteger);
         bool isNotEqualTo (HugeInteger,HugeInteger);
         bool isGreaterThan (HugeInteger,HugeInteger);
         bool isLessThan (HugeInteger,HugeInteger);
         bool isGreaterThanOrEqualTo (HugeInteger,HugeInteger);
         bool isLessThanOrEqualTo (HugeInteger,HugeInteger);
         bool isZero (HugeInteger);
    };
    
    #endif

    Code:
    // implementation of HugeInteger class
    
    #include"HugeInteger.h"
    #include<iostream>
    #include<string> // for strlen
    const iMAX_LENGTH = 40;
    
    using namespace std;
    
    HugeInteger::HugeInteger()
    {
         initializeTheNumber();
         bIsError = false;
    }
    
    void HugeInteger::initializeTheNumber()
    {
         for (int i = 0; i < iMAX_LENGTH; i++)
              cTheNumber[i] = '0';
         cTheNumber[iMAX_LENGTH] = '\0';
    }
    
    int HugeInteger::theLength()
    {
         for (int i = 0; i < sizeof cTheNumber-2; i++) //the last 0 is significant
         {                                             //so preserve it
              if( cTheNumber[i] == '0');
              else
                   break;
         }
         return i;
    }
    
    void HugeInteger::inputHugeInteger()
    {
         //local variables
         char cInputString [iMAX_LENGTH+1]; //the characters read in from the input stream
         bool bFailed  = false;
         int i = 0;
         
         //get the value from the input stream
         cin.getline( cInputString, sizeof cInputString );
    
         //throw out anything exceeding the length
         cin.sync();
         cin.clear();
    
         //see if the string entered is an unsigned integer
         while(i < strlen(cInputString)-1)
         {
              if (!isdigit(cInputString[i]))
                   bFailed = true;
              i++;
         }
    
         //if it's an integer, set the value of iTheNumber
         if (!bFailed)
         {
              i = strlen(cInputString);
    
              while(i >= 0)
              {
                 cTheNumber[iMAX_LENGTH-strlen(cInputString)+i] = cInputString[i];
                 i--;
              }
         }
    }
    
    void HugeInteger::outputHugeInteger()
    {
         for (int x = theLength();  x < sizeof cTheNumber; x++)
              cout << cTheNumber[x]; 
    }
    
    void HugeInteger::addHugeIntegers (HugeInteger a,HugeInteger b)
    {
         int currentPosition = 0;
         int curvala = 0;
         int curvalb = 0;
         int n = 0;
         int value = 0;
         int carry = 0;
    
         currentPosition = iMAX_LENGTH-1;
    
         while (currentPosition >= 0)
         {
              curvalb = b.cTheNumber[currentPosition]-'0';
              curvala = a.cTheNumber[currentPosition]-'0';
              n = curvala + curvalb + carry;
              carry = n/10;
              value = n%10;
              cTheNumber[currentPosition] = value + '0';
              currentPosition--;
         }
    
         if (carry > 0) // an overflow has occurred
         {
              initializeTheNumber();
             bIsError = true;
         }
    }
    
    void HugeInteger::subtractHugeIntegers (HugeInteger a,HugeInteger b)
    {
         int currentPosition = 0;
         int curvala = 0;
         int curvalb = 0;
         int value = 0;
         int borrow = 0;
    
         currentPosition = iMAX_LENGTH-1;
    
         while (currentPosition >= 0)
         {
              curvalb = b.cTheNumber[currentPosition]-'0';
              curvala = a.cTheNumber[currentPosition]-'0';
    
              value = curvala-curvalb+borrow;
              if (value < 0)
              {
                   value += 10;
                   borrow = -1;
              }
              else
                   borrow = 0;
    
              cTheNumber[currentPosition] = value + '0';
              currentPosition--;
         }
    
         if (borrow < 0) // an underflow has occurred
         {
              initializeTheNumber();
             bIsError = true;
         }
    }
    
    void HugeInteger::multiplyHugeIntegers (HugeInteger a, HugeInteger b)
    {
         int currentPositiona = 0;
         int currentPositionb = 0;
         int theDigit = 0;
         int curvala = 0;
         int curvalb = 0;
         int n = 0;
         int value = 0;
         int carry = 0;
         HugeInteger c,d;
    
         currentPositionb = iMAX_LENGTH-1;
         while (currentPositionb >= 0)
         {
              theDigit = b.cTheNumber[currentPositionb]-'0';
             c = c.multiplySingleDigit(a,theDigit,currentPositionb);
            d.addHugeIntegers(d,c);
              currentPositionb--;
         }
    
         for (int i = 0; i < sizeof cTheNumber - 1; i++)
              cTheNumber[i] = d.cTheNumber[i] ;
    }
    
    HugeInteger HugeInteger::multiplySingleDigit (HugeInteger a, int theDigit, int thePosition)
    {
         int currentPosition = 0;
         int curvala = 0;
         int curvalb = 0;
         int n = 0;
         int value = 0;
         int carry = 0;
         HugeInteger c;
    
         currentPosition = iMAX_LENGTH-1;
         while (currentPosition >= 0)
         {
              curvalb = theDigit;
              curvala = a.cTheNumber[currentPosition]-'0';
              n = curvala * curvalb + carry;
              carry = n/10;
              value = n%10;
              c.cTheNumber[currentPosition-(iMAX_LENGTH-1-thePosition)] = value + '0';
              currentPosition--;
         }
         if (carry > 0) // an overflow has occurred
         {
              c.initializeTheNumber();
              c.bIsError = true;
         }     
         return c;
    }
    Code:
    //Program file to test HugeInteger Class
    
    #include "HugeInteger.h"
    
    #include <iostream>
    
    using namespace std;
    
    void pause();
    
    int main()
    {
      HugeInteger a;
      HugeInteger b;
      HugeInteger c;
      HugeInteger d;
    
      cout << "Enter the Huge Integer a: ";
      a.inputHugeInteger();
      cout << "Enter the Huge Integer b: ";
      b.inputHugeInteger();
    
      c.addHugeIntegers(a,b);
      cout << "The Huge Integers added: ";
      c.outputHugeInteger();
      cout << '\n';
    
      c.subtractHugeIntegers(a,b);
      cout << "The Huge Integers subtracted: ";
      c.outputHugeInteger();
      cout << '\n';
    
      c.multiplyHugeIntegers(a,b);
      cout << "The Huge Integers multiplied: ";
      c.outputHugeInteger();
      cout << '\n';
    
    
      pause;
      return 0;
    }
    Can somebody help me?

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    >> char cTheNumber[41];
    Why don't you have it so that it's an integer array? That'll take away the need for subtracting '0' from it if you want to print it out. Just restrict the value of each cell so that it is a value from zero to nine.

    >> cin.getline( cInputString, sizeof cInputString );
    Expecting to get spaces in the number


    >> void HugeInteger::subtractHugeIntegers (HugeInteger a,HugeInteger b)
    it would probably be better to overload an operator to do this for you, so that you could do something like:

    Code:
    HugeInteger A, B;
    A-B;
    ... etc
    and same with multiply.

    It works fine for me, except the subtract part - I get:

    Code:
    A: 11
    B: 22
    A+B: 33
    A-B: 0
    A*B: 242
    It's probably just a small thing to do with the method of subtraction, but it looks good, and it works on MSVS

    EDIT:
    If I enter: 22 and 11, it woks. I think it just has a problem with negative numbers.
    Last edited by twomers; 05-15-2006 at 01:26 AM. Reason: Forgot tags

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Integer problem
    By hobilla in forum C Programming
    Replies: 5
    Last Post: 02-21-2009, 10:01 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  3. Problem with cin. Please help me.
    By Antigloss in forum C++ Programming
    Replies: 17
    Last Post: 06-06-2005, 09:50 AM
  4. A homework problem about C++ (Pointer)
    By joenching in forum C++ Programming
    Replies: 10
    Last Post: 03-14-2005, 03:28 PM
  5. huge string-pointer problem
    By MKashlev in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2002, 02:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21