Okay so I have have been beatting my head againest the wall and I can not get this to work right. I have asked my insttor and she does not know why it will not work righ either.

I read in numbers as characters and then I place them in a dymanic array as integers. This works fine.

When ever I pull them out to add, subtract or multiply them it falls apart. It will only let me have two numbers in the second number for add and sutract and one for multiply and if I use my copy construtor it goes nuts.

Please help.

```
class NumbersC
{
private:
int size; //this is the size of the first set of numbers.
int *num; //A pointer to the first number in a dynamic array.
public:
NumbersC() //Default constructor. Setting all values to zero or NULL to start.
{
size = 0;
num = NULL;
}
~NumbersC()
{
delete[] num; //Deletes num1 to free up memory on the stack.
}
//The copy constructor will not let it have any numbers over two digits long.
NumbersC(const NumbersC& copyNum) //Copy constructor.
{
size = copyNum.size;
num = new int (size);
for(int i = 0; i < size; i++)
{
num[i] = copyNum.num[i];
}
}
NumbersC& operator = (const NumbersC& numbers2) //Overloading the = operator and making it a fried to the class
{
size = numbers2.size;
delete[] num;
num = new int (size);
for(int i = 0; i < size; i++)
{
num[i] = numbers2.num[i];
}
return *this;
}
void setNumber(char numberString[MAX], int i) //creatting and filling num1
{
int x = 0;
size = i; //sets size1 to i which is the count of character entered into numberString
num = new int(size); //Creates the array num1 and sets it size to size1.
//Takes the information out of numberString and put is in num1
while (x < size)
{
num[x] = numberString[x] - '0';
x++;
}
}
//returns the size of each number
int getSize()
{
return size;
}
//Returns each number.
int getNumber()
{
return num[size];
}
//Prints each number to the screen.
void print()
{
for (int c = 0; c < size; c++)
{
cout << num[c];
}
}
//Increase the size of total.num if there is a remainder.
void getIncrease(NumbersC &total, int &remainder)
{
int *tempNumber;
total.size = total.size + 1;
tempNumber = new int (total.size); //Creatting tempNumber to put total into to fix it.
int j = 0;
tempNumber[j] = remainder;
remainder = 0; //setting remainder to zero.
j++;
//Putting the rest of the numbers into tempNumber.
for ( int s = 0; s < total.size; s++)
{
tempNumber[j] = total.num[s]; //Error cannot convert 'NumbersC' to 'char' in assignment.
j++;
}
delete[] total.num; //Deletes total to free up memory on the stack. Because it is the wrong size.
total.num = new int(total.size); //Rebuilds total to the right size.
//Puts the numbers back into total which is now the right size.
for ( int s = 0; s < total.size+1; s++)
{
total.num[s] = tempNumber[s];
}
}
//Subtracts number2 from number1. Called from the operator overload -.
void getSubtract(NumbersC &total, NumbersC &numbers1, NumbersC &numbers2,
int &a, int &b, int y, int x, int &remainder)
{
int number1;
int number2;
//Goes through the numbers one at a time.
while (x < total.size)
{
//If b is less than than zero and the remainder
// is zero set number2 to zero.
if (b < 0 && remainder ==0)
{
number2 = 0;
}
//If the remainder is larger than zero set it to number2 and
//than set remainder to zero.
else if (remainder > 0)
{
number2 = remainder;
remainder = 0;
}
else //Get the number from numbers2.num.
{
number2 = numbers2.num[b];
}
//If a is less than zero set number1 to zero.
if(a < 0)
{
number1 = 0;
}
else //Get the number from numbers1.num.
{
number1 = numbers1.num[a];
}
//If number2 is smaller than number1. Subtract one from b to
//go to the next location is numbers2.num. Take the number
//in numbers2.num and subtract one from it and place it in
//remainder. Then add ten to the number in number2.
if(number2 < number1)
{
b = b - 1;
remainder = numbers2.num[b] - 1;
number2 = number2 + 10;
}
else
{}
total.num[y] = number2 - number1;
cout << total.num[y] << " = " << number2 << " - " << number1 << endl;
//total.num[y] = subTotal;
x++;
y--;
a--;
b--;
}
}
//If subtotal equal to or bigger than 10. It must be reduced through division
//to get sutotal to the last digit in the number and remainder to the
//first digit.
void getSubDivide(int &subTotal, int &remainder)
{
remainder = subTotal / 10;
subTotal = subTotal % 10;
}
friend NumbersC operator +(NumbersC& numbers1, NumbersC& number2);
friend NumbersC operator -(NumbersC& numbers1, NumbersC& number2);
friend NumbersC operator *(NumbersC numbers1, NumbersC number2);
};
int main(int argc, char *argv[])
{
NumbersC number1 , number2;
int i = 0; //Used to go through the loop to put the characters into numberString..
char numberString[MAX]; //This is where the numbers are stored until they are put into the dynamic arrays. num1 and num2.
char letter; //This is used to read the number in at first.
ifstream fin; //The internal name for the file.
char fileName[MAX]; //Is to get the name of the file you are using. The one on your computer in the same folder as the program.
char math; //Math is used for two thngs only in the main functio. 1) To see if you are entring the numbers from the keyboard or file and two to get which math you are doing.
int count = 0; //Used to get the number of numbers in each number. ex. the number 123 has 3 numbers. So it needs to read a 3 from your file first to get the number.
cout << "How would you like to enter the numbers?\n(k)eyboard\n(f)ile: ";
cin >> math;
tolower(math);
//If you are inputting the numbers from the keyboard then if goes in here.
if (math =='k')
{
cin.get(letter); //Do not know why but without it it will skip the first number.
//Start entering number 1.
cout << "\nEnter a number: ";
cin.get(letter);
//Start for isdigit
//Puts the numbers into numberString one at a time.
while (isdigit(letter))
{
numberString[i] = letter;
i++;
cin.get(letter);
}
numberString[i] = '\0'; //Sets the last spot in the array to the NULL or end character.
//end isdigit.
number1.setNumber(numberString, i);
//End entering number 1.
//Start entering number 2.
cout << "\nEnter a number: ";
cin.get(letter);
//Start for isdigit
i = 0; //Reset i to zero to start over.
//Puts the numbers into numberString one at a time.
while (isdigit(letter))
{
numberString[i] = letter;
i++;
cin.get(letter);
}
numberString[i] = '\0'; //Sets the last spot in the array to the NULL or end character.
//end isdigit.
number2.setNumber(numberString, i);
//END NUMBER 2
}
else //For filrs here.
{
cout <<"Please enter the name of the file including the .txt(numbers.txt):\n";
cin >>fileName;
fin.open(fileName); // opens the file call clouds.txt
//This loop is to make sure the file opend right and is working.
if (fin.fail())
{
cout <<"Did not open.";
}else{}
fin >> count;
while(i <count)
{
fin >> numberString[i];
i++;
}
numberString[i] = '\0'; //Sets the last spot in the array to the NULL or end character.
number1.setNumber(numberString, i);
//END NUMBER 1
fin >> count;
i = 0;
while(i <count)
{
fin >> numberString[i];
i++;
}
numberString[i] = '\0'; //Sets the last spot in the array to the NULL or end character.
number2.setNumber(numberString, i);
//END NUMBER 2
fin.close();
}
//Asking what type of math you wish to do.
cout << "\nWhat math would you like to do?\n(a)dd.\n(s)ubtract\n(m)ultiply\n";
cin >> math;
tolower (math);
switch (math)
{
case 'a': //Adding the numbers together.
{
NumbersC sum = number1 + number2; //This works to add numbers.
number1.print();
cout <<" + ";
number2.print();
cout << " = ";
sum.print();
}
break;
case 's': //Subtracting them.
{
NumbersC sum = number1 - number2;
number1.print();
cout << " - ";
number2.print();
cout << " = ";
sum.print();
}
break;
case 'm': //Multiplying them.
{
NumbersC sum = number1 * number2;
cout << "\nIn main.\n";
number1.print();
cout <<" * ";
number2.print();
cout << " = ";
sum.print();
}
break;
default:
NumbersC sum = number1 + number2; //This works to add numbers.
break;
}
cout << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
//Overloading the + operator and making it a fried to the class
//Will not allow any number over two digits in either number
// and if you take out system("PAUSE") it will crash. Strange.
NumbersC operator + ( NumbersC& numbers1, NumbersC& numbers2)
{
NumbersC total;
int number1;
int number2;
int remainder = 0;
int subTotal = 0;
int size1 = numbers1.getSize(); //Sets size1 to numbers1.size;
int size2 = numbers2.getSize(); //Sets size2 to numbers2.size;
int y = 0;
int a = size1-1; //Sets a to size1 - 1. to go throught the array in reverse.
int b = size2-1; //Sets b to size2 - 1. to go throught the array in reverse.
int l = 0;
//If size2 is equal to or grater than size1 set total.size to size2.
if (size1 >= size2)
{
total.size = size1;
}
//If size2 is less than size1 we set total.size to size1.
else
{
total.size = size2;
}
total.num = new int(total.size); //creatting total.num and makeing it the size of total.size.
system("PAUSE"); //If you remove this it will give an error at the end of the program in main.
y = total.size - 1; //sets y tot toal.size - 1 to go through total.num bacwards.
//goes through the numbers and adds the together until l is the same number as total.size.
while (l < total.size)
{
subTotal = 0;
//If a is less than zero we set number1 to zero. So it does the math right if number two is larges in size.
if (a < 0)
{
number1 = 0;
}
else
{
number1 = numbers1.num[a];
}
//If b is less than zero we set number2 to zero. So it does the math right if number one is larges in size.
if ( b < 0)
{
number2 = 0;
}
else
{
number2 = numbers2.num[b];
}
subTotal = number1 + number2 + remainder; //Adds the numbers together.
remainder = 0;
/*******************************************************************
* If subtotal is equal to or greater than ten then we have to do both
* modulas and division on it get the correct subTotal and the remainder to
* be carried over.
*******************************************************************/
if(subTotal >= 10)
{
total.getSubDivide(subTotal, remainder);
}
else{}
total.num[y] = subTotal; //Putting subtotal into total.num at y.
l++;
a--;
b--;
y--;
}
//If remainder is bigger than zero when we are done we have to place it at the start of the array.
if (remainder > 0)
{
total.getIncrease(total, remainder);
}
else{}
return total;
}
//Overloading the + operator and making it a fried to the class
//Will not subtract numbers when number2 is larger than two digit;
NumbersC operator - (NumbersC& numbers1, NumbersC& numbers2)
{
NumbersC total;
int *tempNumber;
int size1 = numbers1.getSize(); //Sets size1 to numbers1.size;
int size2 = numbers2.getSize(); //Sets size2 to numbers2.size;
int number1 = 0;
int number2 = 0;
int remainder = 0; //The number taht one is subtracted from if number1 or number2 is smaller than the other.
int a = size1-1; //Sets a to size1 - 1. to go throught the array in reverse.
int b = size2-1; //Sets b to size2 - 1. to go throught the array in reverse.
int x = 0; //Used to ge through the while loop.
int y = 0; //Goes through total.num
bool done = true;
//If size1 is equal to or greater than size2 set total.size to size1. Else set it to size2.
if (size1 >= size2)
{
total.size = size1;
}
else
{
total.size = size2;
}
y = total.size -1; //Set y to total.size minus one.
total.num = new int(total.size); //creatting total.num and makeing it the size of total.size.
system("PAUSE"); //If I remove this it will fail when any number has more than two digits in it.
//Subtracts number1 from number2.
if (size1 > size2)
{
//While x is less than total.size we go through the numbers on at a time.
while (x < total.size)
{
//If a is less than zero. There is no more numbers in numbers1.num and if remainder is
//equal to zero then it also has no number to be subtracted in it.
//So place a zero in number1.
if (a < 0 && remainder ==0)
{
number1 = 0;
}
else if (remainder > 0) //If the remainder is bigger tan zero we want to use it the next time.
{
number1 = remainder; //Setting number1 to remainder.
remainder = 0; //Resetting remainder to zero so we know it is empty.
}
else //Just get the number for number1 from numbers1.num.
{
number1 = numbers1.num[a];
}
//If b is less than zero that means there are no more numbers in numbers2.num.
//Place a zero in number2.
if(b < 0)
{
number2 = 0;
}
else //Else get the number from numbers2.num
{
number2 = numbers2.num[b];
}
//If number1 is less than number2 we need to add ten to number1 and subtract
//One from the digit on the left and dincreament a by one.
if(number1 < number2)
{
//If a is bigger than or equal to zero we can increase the size of the number1 by ten.
if (a >=0)
{
a--; ////Move a one to the left (dincreament it by one.
remainder = numbers1.num[a] - 1; //Set remainder to equal the number in numbers1.num minus one.
number1 = number1 + 10; //Add ten to the number in number1.
}else{} //Do nothing.
}
if(a<0)
{ //This is done to make the first digit zero to be removed.
total.num[y] = number1 - number2;
break;
}
else //Do the subtraction and place the results in total.num[y]
{
total.num[y] = number1 - number2;
cout << total.num[y] << " = " << number1 << " - " << number2 << endl;
x++;
y--;
a--;
b--;
}
}
}
//If size1 is equal to size2.
else if (size1 == size2)
{
//If the first number in numbers1.num is bigger than the first number in numbers2.num we subtract.
if (numbers1.num[0] >=numbers2.num[0])
{
//While x is less than total.size we go through the numbers on at a time.
while (x < total.size)
{
//If a is less than zero. There is no more numbers in numbers1.num and if remainder is
//equal to zero then it also has no number to be subtracted in it.
//So place a zero in number1.
if (a < 0 && remainder ==0)
{
number1 = 0;
}
else if (remainder > 0) //If the remainder is bigger tan zero we want to use it the next time.
{
number1 = remainder; //Setting number1 to remainder.
remainder = 0; //Resetting remainder to zero so we know it is empty.
}
else //Just get the number for number1 from numbers1.num.
{
number1 = numbers1.num[a];
}
//If b is less than zero that means there are no more numbers in numbers2.num.
//Place a zero in number2.
if(b < 0)
{
number2 = 0;
}
else //Else get the number from numbers2.num
{
number2 = numbers2.num[b];
}
//If number1 is smaller than number2 and a is bigger than or equal to
//zero we get the next number from numbers1.num and subtract one
//from it and set it to remainder add ten to number one.
if(number1 < number2 && a >=0)
{
a = a - 1;
remainder = numbers1.num[a] - 1;
number1 = number1 + 10;
}
else if ((number1 == number2) ||(number1> number2))
{
//With out this empty else if statement it give a strange output.
}
//If we have no numbers left in numbers1.num and remainder is to
//small to subtract number2 from then stop and try a different way.
else
{
cout <<"\nCan not subtract " << number1 << " from " << number2 << endl;
done = false;
}
//If the numbers can be subtracted subtract them and put the answer in total.num[y].
if ( done == true)
{
total.num[y] = number1 - number2;
cout << total.num[y] << " = " << number1 << " - " << number2 << endl;
}
else
{}
x++;
y--;
a--;
b--;
}
//If done is false then try to subtract the numbers the other way.
// by calling the constructor getSubtract
//Reset a, b, x and y back to thier start values.
if (done == false)
{
a = size1-1;
b = size2-1;
y = total.size - 1;
x = 0;
total.getSubtract(total, numbers1, numbers2, a, b, y, x, remainder);
done = true; //set done back to true.
}
}
//Subtract number2 from number1.
// by calling the constructor getSubtract
else
{
total.getSubtract(total, numbers1, numbers2, a, b, y, x, remainder);
}
}
//Switchs the numbers to subtract number2 from number1
// by calling the constructor getSubtract
else
{
total.getSubtract(total, numbers1, numbers2, a, b, y, x, remainder);
}
//This little if statement decreases the size of total if the first number is a zero.
//But it always keeps one digit even if it is a zero.
if (total.num[0] == 0 && total.size > 1)
{
int tempSize = 0; //Just a temp holding place for the answer.
int t = 1; //t is set to one to skip the zero at the beginning of total.num
tempSize = total.size; //tempSize is set to toal.size to get all the places.
total.size = total.size - 1; //Total.size is then reduced by one to drop the zero.
tempNumber = new int(total.size); //Where total is stored while the zero is being dropped.
//This loop drops the zero from the beginning and stores the number in tempNumber.
for (int i = 0; i < total.size; i++)
{
tempNumber[i] = total.num[t];
t++;
}
delete[]total.num; //Delete total.num.
total.num = new int (total.size); //Making a nuw tota.num the size of the new total.
//Puttng the numbers back in total.num
for ( int i = 0; i < total.size; i++)
{
total.num[i] = tempNumber[i];
}
delete[]tempNumber; //Deleteing tempNumber because it is not needed anymore.
}
return total;
}
////Overloading the * operator and making it a fried to the class
//Now can multiply a two digit number by a one digit number.
NumbersC operator * ( NumbersC numbers1, NumbersC numbers2)
{
NumbersC total;
int *tempNumber;
int size1 = numbers1.getSize(); //Sets size1 to numbers1.size;
int size2 = numbers2.getSize(); //Sets size2 to numbers2.size;
int number1 = 0;
int number2 = 0;
int subTotal = 0;
int remainder = 0;
int a = size1-1; //Sets a to size1 - 1. to go throught the array in reverse.
int b = size2-1; //Sets b to size2 - 1. to go throught the array in reverse.
int t = 0; //Used to change the place in total if number 2 has more than one digit.
int y = 0; //Used to keep our place in total.
// If size1 is greater than or equal to size2 then we set tsize (total size) to size1.
if (size1 >= size2)
{
total.size = size1;
}
else //If size2 in bigger than size1 we set tsize (total size) to size2.
{
total.size = size2;
}
total.num = new int(total.size); //creatting total.num and makeing it the size of total.size
if ( size1 > size2)
{ //Going through the for loop in reverse order to have the math come out right.
for (b = size2 - 1; b >=0; b--)
{
y = total.size - 1; //For each loop we reset y to toal.size. to keep the right place in total.num
//t is greater than zero that means we are on the next number in numbers2.num
//We need to leave the last digit in total.num alone and add the subTotal
//To total.num[y] and place it back into total.num[y]
//this is done by subtracting y from t and putting the result back in y.
if (t > 0)
{
y = y - t;
}
number2 = numbers2.num[b]; //sets number2 to the value of numbers2.num[b].
//This loop goes through the first number one digit at a time and multiplies
//them to number2.
for (a = size1 - 1; a >=0; a--)
{
subTotal = 0; //Reset subTotal to zero so we know it's empty.
number1 = numbers1.num[a];
//If t is zero it means we are still on the first number in numbers2.num
//So total.num does not needed to be add to subTotal at this time.
if (t == 0)
{
subTotal = (number1 * number2) + remainder;
cout << subTotal << " = (" << number1 <<" * " << number2 << ") + " << remainder << endl;
remainder = 0;
}
//If t is larger than zero we need to add total.num[y] to subTotal.
else
{
subTotal = (number1 * number2) + remainder + total.num[y];
cout << subTotal << " = (" << number1 <<" * " << number2 << ") + " << remainder << " + " << total.num[y]<< endl;
remainder = 0;
/*******************************************************************
* If subtotal is equal to or greater than ten then we have to do both
* modulas and division on it get the correct subTotal and the remainder to
* be carried over.
*******************************************************************/
if(subTotal >=10)
{
total.getSubDivide(subTotal, remainder);
}
//If remainder is bigger than zero when we are done we have to place it at the start of the array.
if (remainder > 0 && y < 0)
{
total.getIncrease(total, remainder);
}
else{}
}
/*******************************************************************
* If subtotal is equal to or greater than ten then we have to do both
* modulas and division on it get the correct subTotal and the remainder to
* be carried over.
*******************************************************************/
if(subTotal >=10)
{
total.getSubDivide(subTotal, remainder);
}
//If subTotal is bigger than zero when we are done we have to place it at the start of the array.
//Set remainder to subTotal, set subTotal to zero and call getIncrease.
if (subTotal > 0 && y < 0)
{
remainder = subTotal;
subTotal = 0;
total.getIncrease(total, remainder);
}
else //Just put subTotal in toal.num[y].
{
total.num[y] = subTotal;
subTotal = 0; //Reset subtotal to zero to make sure it's empty.
y--;
}
}
t++; //If this is increamented then we change the start point total.num and add what is in total.num into the equation.
}
}
else //We multiply one by two. Just to make life easy and I'm lazy.
{
for (a = size1 - 1; a >=0; a--)
{
y = total.size - 1; //For each loop we reset y to toal.size. to keep the right place in total.num
//t is greater than zero that means we are on the next number in numbers2.num
//We need to leave the last digit in total.num alone and add the subTotal
//To total.num[y] and place it back into total.num[y]
//this is done by subtracting y from t and putting the result back in y.
if (t > 0)
{
y = y - t;
}
number1 = numbers1.num[a]; //sets number1 to the value of numbers1.num[a].
//This loop goes through the first number one digit at a time and multiplies
//them to number1.
for (b = size2 - 1; b >=0; b--)
{
subTotal = 0; //Reset subTotal to zero so we know it's empty.
number2 = numbers2.num[b];
//If t is zero it means we are still on the first number in numbers2.num
//So total.num does not needed to be add to subTotal at this time.
if (t == 0)
{
subTotal = (number2 * number1) + remainder;
cout << subTotal << " = (" << number2 <<" * " << number1 << ") + " << remainder << endl;
remainder = 0;
}
//If t is larger than zero we need to add total.num[y] to subTotal.
else
{
subTotal = (number2 * number1) + remainder + total.num[y];
cout << subTotal << " = (" << number2 <<" * " << number1 << ") + " << remainder << " + " << total.num[y]<< endl;
remainder = 0;
/*******************************************************************
* If subtotal is equal to or greater than ten then we have to do both
* modulas and division on it get the correct subTotal and the remainder to
* be carried over.
*******************************************************************/
if(subTotal >=10)
{
total.getSubDivide(subTotal, remainder);
}
//If remainder is bigger than zero when we are done we have to place it at the start of the array.
if (remainder > 0 && y < 0)
{
total.getIncrease(total, remainder);
}
else{}
}
/*******************************************************************
* If subtotal is equal to or greater than ten then we have to do both
* modulas and division on it get the correct subTotal and the remainder to
* be carried over.
*******************************************************************/
if(subTotal >=10)
{
total.getSubDivide(subTotal, remainder);
}
//If subTotal is bigger than zero when we are done we have to place it at the start of the array.
//Set remainder to subTotal, set subTotal to zero and call getIncrease.
if (subTotal > 0 && y < 0)
{
remainder = subTotal;
subTotal = 0;
total.getIncrease(total, remainder);
}
else //Just put subTotal in toal.num[y].
{
total.num[y] = subTotal;
subTotal = 0; //Reset subtotal to zero to make sure it's empty.
y--;
}
}
t++;
}
}
system("PAUSE"); //If removed will not show answer at end of program. (ie 5 * 5)
//If remainder is bigger than zero when we are done we have to place it at the start of the array.
if (remainder > 0)
{
total.getIncrease(total, remainder);
}
else
{}
// system("PAUSE"); //If removed will not show answer at end of program. (ie 11 * 11) if both left in then 11 * 11 does not work.
return total;
}
```

thanks

Cary