title: Debugging help requested for a small program involving a beginner level class program C++
body: <p>So the deadline for my program has come and gone and I'm left with a lot (re:all) failing tests and some other errors that I am not familiar with. I don't really give a damn about the grade. If I don't understand the content it's all for naught anyway. If you have some time and would like to walk through some of this debugging with me, I'm all ears and would really like to find out what it is that I'm doing incorrectly. I will post all code and the header file below. </p>
  
  <pre><code>#ifndef BIGNUM_H_INCLUDED
  #define BIGNUM_H_INCLUDED
  
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  using namespace std;
  
  class bignum
  {
  public:
  // Constructors.
  bignum();
  bignum(int num_digits);
  bignum(const string &amp;digits);
  bignum(const bignum &amp;other);
  
  // Destructors.
  ~bignum();
  
  // Assignment operator.
  bignum &amp;operator=(const bignum &amp;other);
  
  // Accessors
  int digits() const;
  int as_int() const;
  void bignum::setdigits(const int data[])const;
  string as_string() const;
  void print(ostream &amp;out) const;
  bignum add(const bignum &amp;other) const;
  bignum multiply(const bignum &amp;other) const;
  bool equals(const bignum &amp;other) const;
  int PublicNumberTest;
  
  private:
  // Pointer to a dynamically-allocated array of integers.  The last
  // digit (ones place) is stored in digit[0], the next-to last (tens
  // place) in digit[1], and so on.
  int *digit;
  // Number of digits in the array, not counting leading zeros.
  // Should be less than or equal to the allocated size of digit.
  int ndigits;
  };
  #endif
  
  
  
  
  here is the cpp:
  
  
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;math.h&gt;
  using namespace std;
  #include "bignum.h"
  void pause_215(bool have_newline);
  bool test_bignum();
  
  // Method implementations here.
  bignum::bignum(){
  digit[0];
  }
  
  bignum::~bignum(){
  delete[] digit;
  }
  
  bignum::bignum(int num_digits)
  {
  digit = new int[];
  for (int i = 0; i &lt; num_digits; i++)
      digit[i] = 9;
  }
  
  bignum::bignum(const string &amp;digits){
  bool start = false;
  int NumofDigits = 0;
  int *digit = new int[digits.size()];
  for (int i = 0; i &lt; digits.size(); i++){ //will determine if the char at ith slot                                                can be converted
      if (!isdigit(digits[i])){
          break; //if you can't convert, break
      }
      else{
          int number = digits[i] - '0'; //converts char to #
          if ((number == 0) &amp;&amp; (start = false)) //if the digit is a zero and the sequence has yet to start, do not enter the value
              continue;
          digit[NumofDigits] = number;
          start = true;
          NumofDigits++;
      }
  }
  if (NumofDigits == 0)
      digit[0] = 0;
  }
  
  bignum::bignum(const bignum &amp;other){
  
  ndigits = other.ndigits;
  digit = new int[];
  for (int i = 0; i &lt; ndigits; i++)
      digit[i] = other.digit[i];
  }
  
  bignum&amp; bignum::operator = (const bignum &amp; other){
  
  if (this == &amp;other)
      return *this;
  delete[] digit;
  ndigits = other.ndigits;
  digit = new int[];
  for (int i = 0; i &lt; ndigits; i++)
      digit[i] = other.digit[i];
  
  return *this;
  }
  
  int bignum::digits() const {
  
  return ndigits;
  }
  
  int bignum::as_int() const {  //I was using math.h and pow here, but it was giving a
                                //warning about conflicted data types so I did it the 
  int as_int = 0;           //long way. I haven't tested this yet. Just wrote it. 
  int pow_of_ten = 1;
  for (int i = 0; i &lt; ndigits; i++)
  {   
      as_int = as_int + digit[i] * pow_of_ten;
      for (int k = 0; k == i; k++)
          pow_of_ten = 10 * pow_of_ten;
  }
  return as_int;
  }
  string bignum::as_string() const{
  
  
  char *digit_char = new char[];
  for (int i = 0; i &lt; ndigits; i++){
      digit_char[i] = digit[i];
  }
  string str(digit_char);
  return str;        //this isn't working because I just got this thing to compile at     11:25 and I've had 20 minutes to debug it. ndigits is not getting
  }                       //initialized, which is why the first set         of tests is failing. 
  
  void bignum::print(ostream &amp;out) const{
      for (int i = 0; i &lt; ndigits; i++)
      {
          if (i &gt; 0)
          out &lt;&lt; digit[i];
      }
      out &lt;&lt; endl;
  }
  bignum bignum::add(const bignum&amp; other) const{
  
  int carry = 0;
  int larger = 0;
  int numofdigits = 0;
  int currentDigit = 0;
  int size_other = other.ndigits;
  int *b = new int[];
  int *total = new int[];
  int temp_sum = 0;
  if (size_other &gt; ndigits)
      larger = size_other;
  else
      larger = ndigits;
  for (int i = 0; i &lt; larger + 1; i++){
      int aa = 0;
      int bb = 0;
      if (i &lt; ndigits)
          aa = digit[i];
      if (i &lt; other.ndigits)
          bb = b[i];
      temp_sum = aa + bb + carry;
      currentDigit = temp_sum % 10;
      total[numofdigits] = currentDigit;
      numofdigits++;
      carry = temp_sum / 10;
  }
  bignum sum(numofdigits);
  for (int j = 0; j &lt; numofdigits; j++)
      sum.digit[j] = total[j];
  return sum;
  }
  
  //void bignum::setdigits(const int *temp_row) const{
  //  digit = temp_row; 
  //}
  
  bignum bignum::multiply(const bignum&amp; other) const{
  bignum product;
  int carry = 0;
  int sum = 0;
  int j = 0;
  int *temp_row = new int[];
  int count = 0;
  for (int i = 0; i &lt; ndigits-1; i++){
      carry = 0;
      temp_row[i] = 0;
      for (j; j &lt; other.ndigits - 1; j++){
          sum = digit[i] * other.digit[j] + carry;
          temp_row[i + j] = sum % 10;
          carry = sum / 10;
          count++;
      }
      if (carry&gt;0){
          temp_row[i + j] = carry;
          count++;
      }
      bignum row(count);
      for (int k = 0; k &lt; count; k++){
          row.digit[i] = temp_row[i];
      }
  
          product = product.add(row);
  } return product;
  }
  
  bool bignum::equals(const bignum&amp; other) const{
  bool equal = true;
  if (ndigits != other.ndigits)
      return false;
  for (int i = 0; i &lt; ndigits; i++){
      if (digit[i] != other.digit[i])
          return false;
      else;
  }
  }
  
  // Ordinary (nonmember) functions here.
  int main()
  {
  bool success = test_bignum();
  if (success) {
      cout &lt;&lt; "All tests succeeded, good job!" &lt;&lt; endl;
  } else {
      cout &lt;&lt; "Some tests failed, keep trying!" &lt;&lt; endl;
  }
  
  pause_215(false);
  return 0;
  }
  
  
  bool test_bignum()
  {
  // Current test number.  Be sure to increment this before each test!
  int curr_test = 0;
  // Number of failed tests.
  int failed = 0;
  
  // Test 1: do the default constructor and as_string work?
  curr_test++;
  bignum simple;
  if (simple.as_string() != "0") {
      cerr &lt;&lt; "Test " &lt;&lt; curr_test &lt;&lt; " failed." &lt;&lt; endl;
      failed++;
  }
  
  // Test 2: does the integer constructor work?
  curr_test++;
  bignum nines(9);
  if (nines.as_string() != "999999999") {
      cerr &lt;&lt; "Test " &lt;&lt; curr_test &lt;&lt; " failed." &lt;&lt; endl;
      failed++;
  }
  
  // Test 3: does the string constructor work (correct input)?
  curr_test++;
  bignum hundred("100");
  if (hundred.as_string() != "100") {
      cerr &lt;&lt; "Test " &lt;&lt; curr_test &lt;&lt; " failed." &lt;&lt; endl;
      failed++;
  }
  
  // Test 4: does the string constructor work (bad input)?
  curr_test++;
  bignum sixtyfive("65abcd");
  if (sixtyfive.as_string() != "65") {
      cerr &lt;&lt; "Test " &lt;&lt; curr_test &lt;&lt; " failed." &lt;&lt; endl;
      failed++;
  }
  
  // Test 5: does the string constructor work (all bad input)?
  curr_test++;
  bignum zero("not a number");
  if (zero.as_string() != "0") {
      cerr &lt;&lt; "Test " &lt;&lt; curr_test &lt;&lt; " failed." &lt;&lt; endl;
      failed++;
  }
  
  // Test 6: does the string constructor work (long input)?
  curr_test++;
  bignum huge("123456789123456789");
  if (huge.as_string() != "123456789123456789") {
      cerr &lt;&lt; "Test " &lt;&lt; curr_test &lt;&lt; " failed." &lt;&lt; endl;
      failed++;
  }
  
  // Test 7: does the copy constructor work?
  curr_test++;
  bignum copy(sixtyfive);
  if (copy.as_string() != "65") {
      cerr &lt;&lt; "Test " &lt;&lt; curr_test &lt;&lt; " failed." &lt;&lt; endl;
      failed++;
  }
  
  // Test 8: does the assignment operator work?
  curr_test++;
  simple = hundred;
  if (simple.as_string() != "100") {
      cerr &lt;&lt; "Test " &lt;&lt; curr_test &lt;&lt; " failed." &lt;&lt; endl;
      failed++;
  }
  
  // Test 9: does the destructor work, and do the copy constructor
  // and assignment operator make deep copies?  A failure is likely
  // to crash the program.
  curr_test++;
  {
      bignum secondcopy(copy);
      bignum thirdcopy(secondcopy);
      bignum newcopy("175");
      thirdcopy = hundred;
      copy = newcopy;
      newcopy = bignum("42");
  }
  if (copy.as_string() != "175" || hundred.as_string() != "100") {
      cerr &lt;&lt; "Test " &lt;&lt; curr_test &lt;&lt; " failed." &lt;&lt; endl;
      failed++;
  }
  
  // Test 10: does digits() work?
  curr_test++;
  if (hundred.digits() != 3) {
      cerr &lt;&lt; "Test " &lt;&lt; curr_test &lt;&lt; " failed." &lt;&lt; endl;
      failed++;
  }
  
  // Test 11: does digits() work (zero)?
  curr_test++;
  if (zero.digits() != 1) {
      cerr &lt;&lt; "Test " &lt;&lt; curr_test &lt;&lt; " failed." &lt;&lt; endl;
      failed++;
  }
  
  // Test 12: does multiply() work (no carry)?
  curr_test++;
  bignum multiplicand("123");
  bignum multiplier("12");
  bignum product = multiplicand.multiply(multiplier);
  
  if (product.as_string() != "1476") {
      cerr &lt;&lt; "Test " &lt;&lt; curr_test &lt;&lt; " failed." &lt;&lt; endl;
      failed++;
  }
  
  // Test 13: does multiply() work (with carry)?
  curr_test++;
  bignum mc1("963");
  bignum mc2("82");
  bignum mcp = mc1.multiply(mc2);
  
  if (mcp.as_string() != "78966") {
      cerr &lt;&lt; "Test " &lt;&lt; curr_test &lt;&lt; " failed." &lt;&lt; endl;
      failed++;
  }
  
  // Test 14: does multiply() work (large numbers)?
  curr_test++;
  bignum mh1("12345678900");
  bignum mh2("98765432100");
  bignum mhp = mh1.multiply(mh2);
  
  if (mhp.as_string() != "1219326311126352690000") {
      cerr &lt;&lt; "Test " &lt;&lt; curr_test &lt;&lt; " failed." &lt;&lt; endl;
      failed++;
  }
  
  // TODO: add two more tests for multiply()
  // TODO: add four tests each for add(), as_int(), and equals()
  
  return (failed == 0);
  }
  
  
  {
  if (have_newline) {
      // Ignore the newline after the user's previous input.
      cin.ignore(200, '\n');
  }
  
  // Prompt for the user to press ENTER, then wait for a newline.
  cout &lt;&lt; endl &lt;&lt; "Press ENTER to continue." &lt;&lt; endl;
  cin.ignore(200, '\n');
  }
  </code></pre>
  
  <p>I'm getting an error when I try to use the delete [] for digit in the (!! <em>[copy constructor (deep copy)]</em> THIS IS WRONG - it's in the assignment operator, not the copy constr.!!) and I'm not sure if this is due to a leak (not implementing the destructor somewhere else where i should have) or if it's just syntax, or what, but that is really bothering me. That is really what I'd like to look at first. However, I understand that this approach may not be practical since it may just be best to filter through test cases one at a time in order to put the puzzle together. This is another thing I am interested in. What is the optimum way to debug when you have problems like this? If you provide fixes, I am really more interested in how and why you did what you did than what you did. Please provide reasons for doing things if you can find the time.</p>
  
  <p>lastly, I got a little help here and there from this forum directly, but a lot of help indirectly by looking for things like syntax. Thank you all for devoting time and effort into helping people learn a language that is not necessarily the most user friendly thing on Earth. It seems there is so much to learn about C++ just to get it to <strong>RUN</strong> first and it's a huge time suck, but worth it when you do. Thanks again, for everything.</p>
  
  <p>Brad</p>