Hello LHJay,
After I went to be last I finally had my epiphany.
I think you realize that you can not store a "char" in an "int". What you can do is store the decimal value of a space, i.e., (32), in your "int". Them in the print function just check for (32) and print a space instead of the value.
To start with in "main":
1 2 3 4
|
//in >> num1 >> num2; // <--- Not needed. Done in while loop.
while (in >> num1 >> num2) //(num1 != "-1")
{
|
This is a better way to read a file of unknown length. When you try to read "num1" and it sets eof" the while loop will fail and continue with what is next after the closing brace of while. Also no need for the extra (-1)s in the input file to mark the end of input.
In the if statements:
1 2 3 4 5 6 7 8 9 10 11
|
if (sizeNum1 != sizeNum2)
{
if (sizeNum1 < sizeNum2)
{
diff = sizeNum2 - sizeNum1;
while (diff > 0)
{
top1 = InsertAtHead(top1, 32); // <---Changed.
diff = diff - 1;
}
}
|
Sorry I got tired of loosing track of what "s1" and "s2" were and changed the names. Not required, but a good suggestion and a good idea. It does make the program easier to follow.
In the "printList" function I added to the while loop:
1 2 3 4 5 6 7 8 9
|
while (curr != nullptr)
{
if (curr->data == 32)
out << ' ';
else
out << curr->data;
curr = curr->next;
}
|
This produced the output of:
Given the input of:
80485080443358
4849850549686868696
The output is:
80485080443358 +
4849850549686868696
=======================
And if you reverse the numbers:
4849850549686868696 +
80485080443358
=======================
|
I did make one change in "createNode":
1 2
|
//newNode = (Node*)malloc(sizeof(Node));
newNode = new(Node); // <--- Changed.
|
You should use "new" not "malloc".
Another small change I made in "main":
1 2 3 4 5 6 7 8 9 10 11 12 13
|
Node *top1{ nullptr }, *top2{ nullptr };
//top1 = nullptr;
//top2 = nullptr;
std::string num1;
std::string num2;
int num{}, sizeNum1{}, sizeNum2{}, diff{};
//num = 0;
//s1 = 0;
//s2 = 0;
//diff = 0;
|
If you initialize your variables when they are defined you will not need all those extra lines of code to set the variables to (0) zero.
The end of the while loop and z"main" should look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
//while (top1 != nullptr)
// top1 = freeList(top1);
//while (top2 != nullptr)
// top2 = freeList(top2);
//in >> num1 >> num2; // <--- Not needed now. Done in While condition.
} // End while loop
//while (top1 != nullptr)
// top1 = freeList(top1);
//while (top2 != nullptr)
// top2 = freeList(top2);
}
|
Since I do not know anything about the "freeList" or have access to it I had to comment this bit of code for now. I am only guessing that the while loops will work outside of the first while loop. Having used "new" to create dynamic memory you need "delete" to free that memory when finished with it.
DO NOT count on the program ending and the memory being freed. The same concept holds true for using "malloc".
Hope that helps,
Andy