c++ - Adding HugeInteger String Objects -


i working on problem have add hugeinteger objects store digits in string object. when ran program , input 2 values (ie. 10000000000000000000000000000000000000000000000000000 1), debug assertion failed...expression: string subscript out or range message. think problem due operator += function. not sure how implement carry part/adding strings. appreciate ideas on how fix this.

here's code.

hugeinteger.h

#include <iostream> #include <array> #include <string>  class hugeinteger {     // need offer friendship these 2 functions     friend std::istream & operator >> (std::istream & src, hugeinteger & value);     friend std::ostream & operator << (std::ostream & dest, const hugeinteger & value);  public:     //ctor converts "long long" hugeinteger     hugeinteger(long long value = 0ll); //0ll constant literal value 0     //   of type long long      //ctor converts string hugeinteger     hugeinteger(const char *str);      //convert string hugeinteger     void input(const std::string& str);      //adds rhs lhs (the object pointed "this" pointer) , returns result     hugeinteger & operator +=(const hugeinteger & rhs);      //adds "long long" (rhs) , lhs , puts result temp hugeinteger     //   , returns result     hugeinteger operator +(long long rhs) const;      //adds string (which converted hugeinteger) lhs temp      //       hugeinteger , returns result     hugeinteger operator +(const char * rhs) const;      // overload preincrement operator hugeinteger class     hugeinteger & operator ++ (void);  private:     bool negative;  // true if number negative     std::string hugeint; // each digit stored in string object };  //overloads << , >> operators hugeinteger class std::istream & operator >> (std::istream & src, hugeinteger & value); std::ostream & operator << (std::ostream & dest, const hugeinteger & value); 

hugeinteger.cpp

#include "hugeinteger.h" #include <sstream>  #include <iostream> using namespace std;  hugeinteger::hugeinteger(long long value) {     // set maxdigit digits 0 start     this->negative = false;     if (value < 0ll){ // 0ll constant literal 0 of type long long         this->negative = true;         value = -value; // make value positive                       }      unsigned int = 0;           (; < hugeint.size(); i++)         {             this->hugeint[i] = '0';         }         this->hugeint[i] = '\0';        // convert individual digits of input value hugeinteger     (unsigned int j = hugeint.size() - 1; j >= 0 && value != 0ll; j--)     {         short result = value % 10;         char c = (char)result;         this->hugeint[j] = c;         value /= 10;     }      // test make sure hugeinteger able contain value      if (value != 0ll){         *this = 0ll; // set -0, signal overflow         this->negative = true; //   possibly should increase value assigned     }                          //   maxdigit fix problem. }  // converts string hugeinteger object hugeinteger::hugeinteger(const char *str) {     this->input(str); }  // converts long long hugeinteger , invokes //    hugeinteger::operator +=(const hugeinteger & ) hugeinteger hugeinteger::operator +(long long value) const {     hugeinteger temp = *this;     return temp += (hugeinteger(value)); }   //converts string hugeinteger , invokes  //   hugeinteger::operator +=(const hugeinteger & ) hugeinteger hugeinteger::operator +(const char *str) const {     hugeinteger temp = *this;     return temp += (hugeinteger(str)); }  // adds hugeinteger pointed "this" pointer  //   hugeinteger op. //   calculated result returned hugeinteger & hugeinteger::operator+=(const hugeinteger &op) {       int carry = 0;      (int = op.hugeint.size() - 1; >= 0; i--)     {         this->operator++();         int temp = this->hugeint[i];         temp += carry;          this->hugeint[i] = char(temp);         if (int(this->hugeint[i]) > 9)         {             int temp = int(this->hugeint[i]);             temp -= 10;             this->hugeint[i] = char(temp);             carry = 1;         }          else         {             carry = 0;         }      }      return *this; }  void hugeinteger::input(const std::string& str) {     // assume positive     this->negative = false;      // init. zeros first     unsigned int = 0;      this->hugeint.clear();      while (i < str.size())     {         if (isdigit(str[i]))             this->hugeint += str[i];         i++;     } }  // pre-increment operator hugeinteger & hugeinteger::operator ++ () {     string key = this->hugeint;      istringstream in(key);     int int_key;     in >> int_key;     int_key++;     ostringstream out;     out << int_key;     key = out.str();     this->hugeint = key;      return *this; }  istream & operator>>(istream & input, hugeinteger & value) {     string inputstring;     input >> inputstring;     value.input(inputstring);     return input; }  ostream & operator << (ostream & output, const hugeinteger & value) {     // find first non-zero digit     unsigned int = 0;      if (value.hugeint.size() == 0)     {         cout << '0';     }      while (i < value.hugeint.size()){         if (value.hugeint[i] != '0'){             break;         }         ++i;     }      // if zeros, output single 0     if (i == 40)     {         cout << '0';         return output;     }      // check if need ouput negative sign     if (value.negative){         cout << '-';     }      // output remaining digits     (; < value.hugeint.size(); i++)     {         cout << value.hugeint[i];     }      return output; } 

mainprogram

#include "hugeinteger.h" // include definiton of class hugeinteger using namespace std;  int main() {     hugeinteger a, b, c, d;      // input value & b     cout << "****** test << & >> operators ******\n\n";     cout << "input values , b: ";     cin >> >> b;     cout << "\na = " << << "\nb = " << b;      d = b;      // test += operator     cout << "\n\n****** test += operator ******\n\n";     cout << "a = " << << "\nb = " << b << "\nc = " << c << "\n\n";     cout << "c = b += a\n";     c = b += a;     cout << "\na = " << << "\nb = " << b << "\nc = " << c;     b = d;  // restore b's value      system("pause");     return 0; } // end main 

in hugeinteger::operator+=(),

for (int = op.hugeint.size() - 1; >= 0; i--) {     this->operator++();     int temp = this->hugeint[i]; 

i bounded op.hugeint.size() - 1, may out-of-range this->hugeint since this->hugeint may shorter.


Comments