I am trying to accomodate for negative numbers, and as long as the negative is not being used, I have no problems with my input() ouput() and add() functions. I have tried this two different ways, one with and one without a switch statement to convert negatives. I will show my input() both ways and the char convert() for the latter of the two. With the first -1234 prints -31234 because subtracting '0' results in a -3 for '-', the second way deletes the - completely and -1234 prints 1234.
and the second design is:
Code:
void HugeInteger::input()
{
char line[41]; //characters read in to hold integer values
cout << "\nEnter Huge Integer (Max: 40 digits): ";
cin.getline(line, sizeof line);
inputSize = strlen(line); //determine size of users integer
if (inputSize > arraySize) //make sure no more than 40 digits where entered
cout << "\nThe integer entered was to long. Array not initialized." << endl;
else
for (int loopSize = 0; loopSize < inputSize; loopSize++)
//subtract ASCII value for '0' to get raw value
//store digits at end of 40 digit array all non initialized parts of array stay a 0
bigNumber[(arraySize - 1) - loopSize] = line[(inputSize- 1) - loopSize] - '0';
}
Code:
void HugeInteger::input()
{
char line[41]; //characters read in to hold integer values
cout << "\nEnter Huge Integer (Max: 40 digits): ";
cin.getline(line, sizeof line);
inputSize = strlen(line); //determine size of users integer
if (inputSize > arraySize) //make sure no more than 40 digits where entered
cout << "\nThe integer entered was to long. Array not initialized." << endl;
else
for (int loopSize = 0; loopSize < inputSize; loopSize++)
//subtract ASCII value for '0' to get raw value
//store digits at end of 40 digit array all non initialized parts of array stay a 0
bigNumber[(arraySize - 1) - loopSize] = line[(inputSize - 1) - loopSize];
for (int digitIndex = 0; digitIndex < arraySize; digitIndex++)
bigNumber[digitIndex] = intConvert(bigNumber[digitIndex]);
}//end input function
int HugeInteger::intConvert(char toConvert)
{
switch(toConvert)
{
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
case '0': return 0;
default: return 0;
}
}
Comment