Hi, I have been working on this program for a while, and it is just getting plain frustrating. I'm trying to make a class(called numberType) which can take a number with up to 100 digits using a 100 integer character array and then be able to add that class with another one.

To do this, I made a class function that takes the number you give it, and reverses the order as shown below so that I can later on add two classes together.

void numberType::setNumber(int number)
{
     for (int i = 0; num[i] != -99; i++)
     {
         num[i] = (number % 10);
         number = number / 10;
         
         if (number == 0)
         {
               num[i+1] = -99;
         }
     }
}

num[] is the 100 character array that I am using. In the code I also use the number "-99" as a form of a null character so that when it hits that number in my array, it will stop printing.

My problem however is I can do everything except shift the numbers over 1 value if two numbers added together is larger than 9. For instance, if I have one numberType of 500, and another of 400, it will be displayed as 009, but I try to add 500 + 500, it does not display the desired result of 0001. Even more, I've been trying to make it so that it stops once it reaches a -99 value, but it just won't cooperate and adds them anyways. Gah, now I'm talking to my code as if it was some sort of retarded monkey.

Here's the function that is driving me bonkers. If you could help me why this darn thing isn't doing what I want it to do, I'd gladly appreciate it.

numberType numberType::operator+
(const numberType& number) const
{
       numberType temp;
       
       for (int i = 0; temp.num[i] != -99 && i < 100; i++)
       {
           temp.num[i] = num[i] + number.num[i];
           
           if (temp.num[i] >= 10)
           {
                           temp.num[i] = temp.num[i] - 10;
                           
                           if (temp.num[i+1] == -99)
                           { 
                           temp.num[i+1] = 1;
                           temp.num[i+2] = -99;
                           }
                           
                           else
                           {
                               temp.num[i+1] += 1;
                           }
           }
       }
       
       return temp;
}

Edited 5 Years Ago by w1mark: n/a

if (temp.num[i+1] == -99)

I suspect the problem is in the above line. It's difficult to say for sure, but how do you know where to put -99 in temp.num? I suspect there is no -99 in temp.num so the above line won't work.

On top of that you need some way to carry over a one for any index of num not just the last index.

Ah yea you are right, that is a problem. This is one of the first times that I've overloaded an operator for a class and I thought I was doing something wrong.

My default constructor for numberType just sets all the numbers of the array to 0, so checking if temp.num's value is -99 is pretty much pointless.

I changed it to this and it functions better, but it seems to be acting a little quirky. For instance, when I try to make it add 999 + 999, it should output 8991 but it says 8881. Do you have a clue why it's doing that?

numberType numberType::operator+
(const numberType& number) const
{
       numberType temp;
       
       for (int i = 0; num[i] != -99 && i < 100; i++)
       {
           temp.num[i] = num[i] + number.num[i];
           
           if (temp.num[i] >= 10)
           {
                           temp.num[i] = temp.num[i] - 10;
                           
                           if (num[i+1] == -99 || number.num[i+1] == -99)
                           { 
                           temp.num[i+1] = 1;
                           temp.num[i+2] = -99;
                           }
                           
                           else
                           {
                               temp.num[i+1] += 1;
                           }
           }
       }
       
       return temp;
}

Ugh, never mind, after a bunch of tweaking, I finally got it to working right. Had to make one hella of a long for condition though. The for loop adds each of the integers separately and terminates after both of the arrays are both -99 and the last digit in temp is carried over.

numberType numberType::operator+
(const numberType& number) const
{
       numberType temp;
       int i;
       for (i = 0; (num[i] != -99 || number.num[i] != -99 || temp.num[i] != 0) && i < 100; i++)
       {
           
           if (num[i] != -99)
           {
                temp.num[i] += num[i];
           }
           
           if (number.num[i] != -99)
           {
                             temp.num[i] += number.num[i];
           }
           
           if (temp.num[i] >= 10)
           {
                           temp.num[i+1] += 1;
                           temp.num[i] = temp.num[i] % 10;
           } 
       }
       
       temp.num[i] = -99;
       
       return temp;
}
This question has already been answered. Start a new discussion instead.